Python 比较两个列表并只打印差异?(异或两个列表)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16312730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Comparing two lists and only printing the differences? (XORing two lists)
提问by user2314520
I'm trying to create a function that takes in 2 lists and returns the list that only has the differences of the two lists.
我正在尝试创建一个函数,该函数接受 2 个列表并返回仅具有两个列表差异的列表。
Example:
例子:
a = [1,2,5,7,9]
b = [1,2,4,8,9]
The result should print [4,5,7,8]
结果应该打印 [4,5,7,8]
The function so far:
到目前为止的功能:
def xor(list1, list2):
list3=list1+list2
for i in range(0, len(list3)):
x=list3[i]
y=i
while y>0 and x<list3[y-1]:
list3[y]=list3[y-1]
y=y-1
list3[y]=x
last=list3[-1]
for i in range(len(list3) -2, -1, -1):
if last==list3[i]:
del list3[i]
else:
last=list3[i]
return list3
print xor([1,2,5,7,8],[1,2,4,8,9])
The first for loop sorts it, second one removes the duplicates. Problem is the result is
[1,2,4,5,7,8,9]not [4,5,7,8], so it doesn't completely remove the duplicates? What can I add to do this.
I can't use any special modules, .sort, set or anything, just loops basically.
第一个 for 循环对其进行排序,第二个循环删除重复项。问题是结果
[1,2,4,5,7,8,9]不是[4,5,7,8],所以它没有完全删除重复项?我可以添加什么来做到这一点。我不能使用任何特殊模块,.sort、set 或任何东西,基本上只是循环。
采纳答案by sashkello
You basically want to add an element to your new list if it is present in one and not present in another. Here is a compact loop which can do it. For each element in the two lists (concatenate them with list1+list2), we add element if it is not present in one of them:
如果一个元素出现在另一个列表中而不出现在另一个列表中,您基本上想将它添加到新列表中。这是一个可以做到的紧凑循环。对于两个列表中的每个元素(用 连接它们list1+list2),如果元素不存在于其中之一,我们添加元素:
[a for a in list1+list2 if (a not in list1) or (a not in list2)]
You can easily transform it into a more unPythonic code with explicit looping through elements as you have now, but honestly I don't see a point (not that it matters):
您可以像现在一样通过显式循环将其转换为更非 Python 的代码,但老实说,我没有看到任何意义(这并不重要):
def xor(list1, list2):
outputlist = []
list3 = list1 + list2
for i in range(0, len(list3)):
if ((list3[i] not in list1) or (list3[i] not in list2)) and (list3[i] not in outputlist):
outputlist[len(outputlist):] = [list3[i]]
return outputlist
回答by Patashu
Note: This is really unpythonic and should only be used as a homework answer :)
注意:这真的不符合pythonic,只能用作作业答案:)
After you have sorted both lists, you can find duplicates by doing the following:
对两个列表进行排序后,您可以通过执行以下操作来查找重复项:
1) Place iterators at the start of A and B
1) 将迭代器放在 A 和 B 的开头
2) If Aitr is greater than Bitr, advance Bitr after placing Bitr's value in the return list
2)如果Aitr大于Bitr,将Bitr的值放入返回列表后,将Bitr提前
3) Else if Bitr is greater than Aitr, advance Aiter after placing Aitr's value in the return list
3)否则如果Bitr大于Aitr,将Aitr的值放入返回列表后推进Aiter
4) Else you have found a duplicate, advance Aitr and Bitr
4) 否则你发现重复,提前 Aitr 和 Bitr
回答by Sheng
Use set is better
使用套装更好
>>> a = [1,2,5,7,9]
>>> b = [1,2,4,8,9]
>>> set(a).symmetric_difference(b)
{4, 5, 7, 8}
Thanks to @DSM, a better sentence is:
感谢@DSM,更好的句子是:
>>> set(a)^set(b)
These two statements are the same. But the latter is clearer.
这两种说法是一样的。但后者更清晰。
Update: sorry, I did not see the last requirement: cannot use set. As far as I see, the solution provided by @sashkello is the best.
更新:抱歉,我没有看到最后一个要求:不能使用 set。在我看来,@sashkello 提供的解决方案是最好的。
回答by John La Rooy
Simple, but not particularly efficient :)
简单,但不是特别有效:)
>>> a = [1,2,5,7,9]
>>> b = [1,2,4,8,9]
>>> [i for i in a+b if (a+b).count(i)==1]
[5, 7, 4, 8]
Or with "just loops"
或者“只是循环”
>>> res = []
>>> for i in a+b:
... c = 0
... for j in a+b:
... if i==j:
... c += 1
... if c == 1:
... res.append(i)
...
>>> res
[5, 7, 4, 8]
回答by Paul Hankin
This code works assuming you've got sorted lists. It works in linear time, rather than quadratic like many of the other solutions given.
假设您有排序列表,此代码有效。它在线性时间内工作,而不是像许多其他给出的解决方案那样是二次的。
def diff(sl0, sl1):
i0, i1 = 0, 0
while i0 < len(sl0) and i1 < len(sl1):
if sl0[i0] == sl1[i1]:
i0 += 1
i1 += 1
elif sl0[i0] < sl1[i1]:
yield sl0[i0]
i0 += 1
else:
yield sl1[i1]
i1 += 1
for i in xrange(i0, len(sl0)):
yield sl0[i]
for i in xrange(i1, len(sl1)):
yield sl1[i]
print list(diff([1,2,5,7,9], [1,2,4,8,9]))
回答by Satish Bellapu
Try this,
尝试这个,
a = [1,2,5,7,9]
b = [1,2,4,8,9]
print set(a).symmetric_difference(set(b))

