我有一个数组[1,2,3,4,5,6,1,2,3,4],我想返回5,6。
如果我使用set(my_array),我会得到1,2,3,4,5,6。有没有pythonic方式来做到这一点。感谢。
非常感谢任何帮助。
答案 0 :(得分:0)
#List of data which has every item repeated except for 5 and 6
lst= [1,2,3,4,5,6,1,2,3,4]
#This list comprehension prints a value in the list if the value only occurs once.
print [x for x in lst if lst.count(x)==1]
#Output
[5, 6]
答案 1 :(得分:0)
您可以使用适当命名的过滤器方法:
>>> i = [1,2,3,4,5,6,1,2,3,4]
>>> filter(lambda x: i.count(x) == 1, i)
[5, 6]