Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.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 - Fatal编程技术网

比较两个列表-Python

比较两个列表-Python,python,list,Python,List,好的,我有两个清单,清单1和清单2。我想找到列表1和列表2中的所有项目,并将它们从列表1中删除。我想到的第一种方法是循环列表1,然后循环列表2,看看它是否在列表2中,但如果按比例放大,这似乎很慢,效率也很低。有没有更有效的方法 此外,如果有帮助的话,这些列表将按字母顺序排列(它们是字符串) 我正在使用python,但我也从一般编程的角度考虑 list1 = ['bar','foo','hello','hi'] list2 = ['alpha','bar','hello','xam'] 在pyt

好的,我有两个清单,清单1和清单2。我想找到列表1和列表2中的所有项目,并将它们从列表1中删除。我想到的第一种方法是循环列表1,然后循环列表2,看看它是否在列表2中,但如果按比例放大,这似乎很慢,效率也很低。有没有更有效的方法

此外,如果有帮助的话,这些列表将按字母顺序排列(它们是字符串)

我正在使用python,但我也从一般编程的角度考虑

list1 = ['bar','foo','hello','hi']
list2 = ['alpha','bar','hello','xam']

在python中,列表1将变成
['foo','hi']

,您可能需要使用一个集合:

intersection = set(list1).intersection(list2)
这将返回一个
,该集将破坏订单(除其他外),但您始终可以使用该集在之后过滤
列表1

list1 = [x for x in list1 if x not in intersection]

如果确实要使用集合,则交点最有用。正如评论中指出的,如果您根本不想要一套,那么实际上没有必要:

set2 = set(list2)
list1 = [x for x in list1 if x not in set2]

在python中,您可能需要使用一个集合:

intersection = set(list1).intersection(list2)
这将返回一个
,该集将破坏订单(除其他外),但您始终可以使用该集在之后过滤
列表1

list1 = [x for x in list1 if x not in intersection]

如果确实要使用集合,则交点最有用。正如评论中指出的,如果您根本不想要一套,那么实际上没有必要:

set2 = set(list2)
list1 = [x for x in list1 if x not in set2]

使用
集合
获取两者之间的差异:

list1 = ['bar','foo','hello','hi']
list2 = ['alpha','bar','hello','xam']

set1 = set(list1)
set2 = set(list2)
set1 - set2
产出:

set(['hi', 'foo'])
['foo', 'hi']
正如@chepner所指出的,使用set.difference,只需要将第一个转换为一个集合

set1.difference(list2)

如果顺序很重要,则将其中一个列为一组,并将另一个与之进行比较:

set2 = set(list2)
[x for x in list1 if x not in set2]
产出:

set(['hi', 'foo'])
['foo', 'hi']

使用
集合
获取两者之间的差异:

list1 = ['bar','foo','hello','hi']
list2 = ['alpha','bar','hello','xam']

set1 = set(list1)
set2 = set(list2)
set1 - set2
产出:

set(['hi', 'foo'])
['foo', 'hi']
正如@chepner所指出的,使用set.difference,只需要将第一个转换为一个集合

set1.difference(list2)

如果顺序很重要,则将其中一个列为一组,并将另一个与之进行比较:

set2 = set(list2)
[x for x in list1 if x not in set2]
产出:

set(['hi', 'foo'])
['foo', 'hi']

这是一个使用通用编程方法的解决方案,不使用集合,也不特别优化。它依赖于正在排序的两个列表

list1 = ['a', 'b', 'd', 'f', 'k']
list2 = ['c', 'd', 'i']
result = []

i1 = 0
i2 = 0
while i1 < len(list1) and i2 < len(list2):
    # invariants:
    #    list1[i1] not in list2[:i2], and
    #    result == (list1[:i1] with elements of list2[:i2] omitted)
    #
    if list1[i1] < list2[i2]:
        # By assumption, list1[i1] not in list2[:i2],
        # and because list2 is sorted, the true 'if' condition
        # implies that list1[i1] isn't in list2[i2:] either;
        # that is, it isn't in list2 at all.
        result.append(list1[i1])
        i1 += 1
    elif list1[i1] > list2[i2]:
        # can't decide membership of list1[i1] yet;
        # advance to next element of list2 and loop again
        i2 += 1
    else:
        # list1[i1] == list2[i2], so omit this element
        i1 += 1
        i2 += 1

# Add any remaining elements of list1 to tail of result
if i1 < len(list1):
    result.extend(list1[i1:])

print(result)
list1=['a','b','d','f','k']
列表2=['c'、'd'、'i']
结果=[]
i1=0
i2=0
而i1list2[i2]:
#尚未决定列表1[i1]的成员资格;
#前进到列表2的下一个元素并再次循环
i2+=1
其他:
#list1[i1]==list2[i2],因此省略此元素
i1+=1
i2+=1
#将列表1的任何剩余元素添加到结果的尾部
如果i1
结果:
['a','b','f','k']

这里有一个使用通用编程方法的解决方案,不使用集合,也不特别优化。它依赖于正在排序的两个列表

list1 = ['a', 'b', 'd', 'f', 'k']
list2 = ['c', 'd', 'i']
result = []

i1 = 0
i2 = 0
while i1 < len(list1) and i2 < len(list2):
    # invariants:
    #    list1[i1] not in list2[:i2], and
    #    result == (list1[:i1] with elements of list2[:i2] omitted)
    #
    if list1[i1] < list2[i2]:
        # By assumption, list1[i1] not in list2[:i2],
        # and because list2 is sorted, the true 'if' condition
        # implies that list1[i1] isn't in list2[i2:] either;
        # that is, it isn't in list2 at all.
        result.append(list1[i1])
        i1 += 1
    elif list1[i1] > list2[i2]:
        # can't decide membership of list1[i1] yet;
        # advance to next element of list2 and loop again
        i2 += 1
    else:
        # list1[i1] == list2[i2], so omit this element
        i1 += 1
        i2 += 1

# Add any remaining elements of list1 to tail of result
if i1 < len(list1):
    result.extend(list1[i1:])

print(result)
list1=['a','b','d','f','k']
列表2=['c'、'd'、'i']
结果=[]
i1=0
i2=0
而i1list2[i2]:
#尚未决定列表1[i1]的成员资格;
#前进到列表2的下一个元素并再次循环
i2+=1
其他:
#list1[i1]==list2[i2],因此省略此元素
i1+=1
i2+=1
#将列表1的任何剩余元素添加到结果的尾部
如果i1
结果:
['a','b','f','k']

使用问题的样本输出,这给出了
集合(['bar','hello'])
,这不是海报想要的。是否依赖于此版本?(我在2.7.6?)实际上,你不需要交叉点,因为你循环的所有元素都来自
list1
。@wnnmaw——我最初误解了OP的问题。我更新后在列表comp中添加了一个
not
,以获得预期的输出。@tobias_k--这一点很好。在一般情况下,我习惯于使用集合,而不仅仅是使用它来过滤列表。我已经更新了。使用问题的示例输出,这给出了
set(['bar','hello'])
,这不是海报想要的。是否依赖于此版本?(我在2.7.6?)实际上,你不需要交叉点,因为你循环的所有元素都来自
list1
。@wnnmaw——我最初误解了OP的问题。我更新后在列表comp中添加了一个
not
,以获得预期的输出。@tobias_k--这一点很好。在一般情况下,我习惯于使用集合,而不仅仅是使用它来过滤列表。我已经更新了。
-
difference
方法的运算符形式,因此您可以使用
set(list1).difference(list2)
来避免额外遍历
list2
。谢谢,为此进行了编辑。但不幸的是,您不能执行
set-list
-
difference
方法的运算符形式,因此您可以使用
set(list1).difference(list2)
来避免对
list2
进行额外的遍历。谢谢,为此进行了编辑。但不幸的是,你不能
set-list
你可以尝试
list(set(list1)-(set(list1)&set(lis