Python 如何使用';设置';?
我希望在比较时能够保持列表的顺序。请注意,当我比较list1和list2时,它会返回8,然后返回7(相反)。我如何让它维持秩序Python 如何使用';设置';?,python,list,set,Python,List,Set,我希望在比较时能够保持列表的顺序。请注意,当我比较list1和list2时,它会返回8,然后返回7(相反)。我如何让它维持秩序 >>> list1 = ['1','2','3','4','7','8'] >>> list2 = ['1','2','3','4','5','6'] >>> list(set(list2) - set(list1)) ['5', '6'] >>> list(set(list1) - se
>>> list1 = ['1','2','3','4','7','8']
>>> list2 = ['1','2','3','4','5','6']
>>> list(set(list2) - set(list1))
['5', '6']
>>> list(set(list1) - set(list2))
['8', '7']
OrderedDict
可以很好地替代保留顺序的集合,因为键视图的设置如下:
>>> from collections import OrderedDict
>>> list1 = ['1','2','3','4','7','8']
>>> list2 = ['1','2','3','4','5','6']
>>> OrderedDict.fromkeys(list1).keys() - OrderedDict.fromkeys(list2).keys()
{'7', '8'}
>>> OrderedDict.fromkeys(list2).keys() - OrderedDict.fromkeys(list1).keys()
{'5', '6'}
严格地说,这可能仍然在使用CPython的实现细节。但是列表的理解不是,它们仍然是O(n):
您可以使用列表:
list1 = ['1','2','3','4','7','8']
list2 = ['1','2','3','4','5','6']
set1 = set(list1) # convert to set for faster membership testing
result = [x for x in list2 if x not in set1]
# result: ['5', '6']
[x for x, y in zip(b, a) if x != y]
# ['5', '6']
[x for x, y in zip(a, b) if x != y]
# ['7', '8']
但是,这将包括重复的元素:
>>> list1 = [1]
>>> list2 = [1, 2, 2]
>>> set1 = set(list1)
>>> [x for x in list2 if x not in set1]
[2, 2]
如果不需要重复,只需将列表转换为循环,并跟踪您已经遇到的所有元素:
list1 = [1]
list2 = [1, 2, 2]
set1 = set(list1)
result = []
for x in list2:
if x in set1:
continue
result.append(x)
set1.add(x)
# result: [2]
虽然这不会对集合使用
-
运算符,但会保留原始列表的顺序
list1 = ['1','2','3','4','7','8']
list2 = ['1','2','3','4','5','6']
set_list2 = set(list2)
result = []
for item in list1:
if not item in set_list2:
result.append(item)
set_list2.add(item) # to avoid duplicates in result
print(result)
# ['7', '8']
您可以使用列表理解来找出差异。以下是您的操作方法:
[x代表列表1中的x,如果x不在列表2中]
这将产生['7','8']
如果x不在列表1中,您可以执行[x代表列表2中的x]
,这将导致['5','6']
如果原始列表包含重复项,您可以使用
[x表示列表1中的x,如果x不在列表2和列表1中。计数(x)==1]
根据定义,“集合”是无序的。如果要对元素进行排序,请尝试使用列表操作。您可以使用from numpy,例如:
import numpy as np
list1 = ['1','2','3','4','7','8']
list2 = ['1','2','3','4','5','6']
new_list_one = np.setdiff1d(list2, list1)
new_list_two = np.setdiff1d(list1, list2)
print(new_list_one) ['5' '6']
print(new_list_two) ['7' '8']
可能是最幼稚的方法:
def ordered_diff(a, b):
seen = set(b)
result = []
for ele in a:
if ele not in seen:
result.append(ele)
seen.add(ele)
return result
list1 = ['1','2','3','4','7','8','7','8']
list2 = ['1','2','3','4','5','6','5','6']
print ordered_diff(list1, list2)
print ordered_diff(list2, list1)
结果:
['7', '8']
['5', '6']
在这种情况下,只需使用列表:
list1 = ['1','2','3','4','7','8']
list2 = ['1','2','3','4','5','6']
set1 = set(list1) # convert to set for faster membership testing
result = [x for x in list2 if x not in set1]
# result: ['5', '6']
[x for x, y in zip(b, a) if x != y]
# ['5', '6']
[x for x, y in zip(a, b) if x != y]
# ['7', '8']
一般来说,考虑(vs.代码> zip ),特别是不同长度的列表。您是否想要列表中给出的顺序,或者您想要SET差异的结果排序吗?对,所以我希望从原来的列表中维护订单。换句话说,预期输出应该是['7','8'],而不是。。。但是,我也需要应用于字符串(保持列表的顺序),而不是按数字“排序”。在Python 3.6+中的相关文章中,dict插入是按顺序排列的,因此您可以使用
dict.fromkeys()
@pylang不太合适。Python 3.7+或CPython 3.6+。设置差异解决方案不起作用。结果的顺序是随机的。@Aran Fey我找不到一个Python版本,其中odict\u键
视图随机返回(尽管可能未指定)。不管怎样,我并没有把它作为一个完整的解决方案,只是演示了视图的设置方式。@Aran Fey。没错,但我认为细节有点迂腐。我怀疑python
标记本质上是指CPython实现。不管怎样,谢谢。如果原始列表包含重复列表怎么办?@Akavall你必须问OP在这种情况下会发生什么。在我看来,OP想要一个保留顺序的集合差异。@Akavall然后你对集合做了类似的事。计数器而不是集合。看起来不错,我不是投反对票的人,所以我不能撤销它!我认为更新set_list2
以跟踪添加到result
的项目,因此不再一次添加相同的元素,这再次符合OPs问题的精神。