Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在多个列表中查找公共元素,并删除在Python中匹配的数据_Python_List_Set - Fatal编程技术网

在多个列表中查找公共元素,并删除在Python中匹配的数据

在多个列表中查找公共元素,并删除在Python中匹配的数据,python,list,set,Python,List,Set,我有一个包含很多页面的文档,我正在尝试提取每页的前3行 我这样做,我的输出是多个列表,例如: ['hello','','data'] ['hello','','data'] ['test','','data'] 我想删除任何匹配列表,例如,第1页和第2页有相同的3行。我想从相关页面的原始文档中删除这三行。我怎么能这样做呢 到目前为止,我已经尝试使用集合和交集函数。例如: for item in line_list: common = list(set(line_list[0]).int

我有一个包含很多页面的文档,我正在尝试提取每页的前3行

我这样做,我的输出是多个列表,例如:

['hello','','data']
['hello','','data']
['test','','data']
我想删除任何匹配列表,例如,第1页和第2页有相同的3行。我想从相关页面的原始文档中删除这三行。我怎么能这样做呢

到目前为止,我已经尝试使用集合和交集函数。例如:

for item in line_list:
    common = list(set(line_list[0]).intersection(line_list[2:]))
    print (common)

我在这里得到的都是空名单。我想这可能是我的语法,但不确定。有人能提供建议吗?

设置。交集
不接受iterable的iterable(看起来您正试图通过),但它允许任意数量的参数,它希望这些参数是iterable

尝试:


谷歌搜索“splat operator python”,了解有关
*
功能的更多信息。

python中有一个名为
set
的函数,它返回一个具有唯一值或列表元组的集合。问题是你有一个列表。因此,为了做到这一点,您必须将列表转换为元组,然后使用set获取唯一列表,然后再次将其转换为列表。你就是这样做的:

a = ['hello','','data']
b = ['hello','','data']
c = ['test','','data']

common = [list(x) for x in set(tuple(x) for x in [a, b, c])]

假设您知道复制的位置,您可以以智能的方式进行切片以消除冗余。 您可以使用readlines()命令:


lines\u无冗余=所有\u行[3::]

您只想保留['hello'、''data']、['test'、''data']?我真的不明白你想达到什么目的。不,我想从我的整页数据中删除常用列表。因此,如果没有带有
['test','','data']
的公共页面,则此列表可以保留,循环将检查文档的下一页。这可能无法回答您试图总体执行的操作,但它应该向您指出
set.intersection
的正确用法,这样您至少可以在特定情况下正确评估它的有用性。这种方法并不适用于所有情况,只适用于您确切知道副本所在的情况。正如我在回答中提到的:“假设您知道副本所在的位置[…]”,你当然可以这样做。
a = ['hello','','data']
b = ['hello','','data']
c = ['test','','data']

common = [list(x) for x in set(tuple(x) for x in [a, b, c])]