仅从两个列表中获取唯一元素 python
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28444561/
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
Get only unique elements from two lists python
提问by user3683587
if i have two lists (may be with different len):
如果我有两个列表(可能有不同的 len):
x = [1,2,3,4]
f = [1,11,22,33,44,3,4]
result = [11,22,33,44]
im doing:
我正在做:
for element in f:
if element in x:
f.remove(element)
im getting
我越来越
result = [11,22,33,44,4]
回答by Lee Daniel Crocker
Your method won't get the unique element "2". What about:
您的方法不会获得唯一元素“2”。关于什么:
list(set(x).intersection(f))
回答by gdanezis
If you want the unique elements from both lists, this should work:
如果您想要两个列表中的唯一元素,这应该有效:
x = [1,2,3,4]
f = [1,11,22,33,44,3,4]
res = list(set(x+f))
print(res)
# res = [1, 2, 3, 4, 33, 11, 44, 22]
回答by user3683587
v_child_value = [{'a':1}, {'b':2}, {'v':22}, {'bb':23}]
shop_by_cat_sub_cats = [{'a':1}, {'b':2}, {'bbb':222}, {'bb':23}]
unique_sub_cats = []
for ind in shop_by_cat_sub_cats:
if ind not in v_child_value:
unique_sub_cats.append(ind)
unique_sub_cats = [{'bbb': 222}]
unique_sub_cats = [{'bbb': 222}]
回答by Mike Scotty
Based on the clarification of this question in a new (closed) question:
基于在一个新的(封闭的)问题中对这个问题的澄清:
If you want all items from the second list that do not appear in the first list you can write:
如果您想要第二个列表中未出现在第一个列表中的所有项目,您可以编写:
x = [1,2,3,4]
f = [1,11,22,33,44,3,4]
result = set(f) - set(x) # correct elements, but not yet in sorted order
print(sorted(result)) # sort and print
# Output: [11, 22, 33, 44]
回答by Javed
if you want to get only unique elements from the two list then you can get it by..
如果您只想从两个列表中获取唯一元素,那么您可以通过..
a=[1,2,3,4,5]
b= [2,4,1]
list(set(a) - set(b))
OP:- [3, 5]
回答by Yegor Kryukov
Using this piece of Python's documentationon sets:
在集合上使用这段Python 文档:
>>> # Demonstrate set operations on unique letters from two words
...
>>> a = set('abracadabra')
>>> b = set('alacazam')
>>> a # unique letters in a
{'a', 'r', 'b', 'c', 'd'}
>>> a - b # letters in a but not in b
{'r', 'd', 'b'}
>>> a | b # letters in a or b or both
{'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'}
>>> a & b # letters in both a and b
{'a', 'c'}
>>> a ^ b # letters in a or b but not both
{'r', 'd', 'b', 'm', 'z', 'l'}
I came up with this piece of code to obtain unique elements from two lists:
我想出了这段代码来从两个列表中获取唯一元素:
(set(x) | set(f)) - (set(x) & set(f))
or slightly modified to return list
:
或稍作修改以返回list
:
list((set(x) | set(f)) - (set(x) & set(f))) #if you need a list
Here:
这里:
|
operator returns elements inx
,f
or both&
operator returns elements in bothx
andf
-
operator subtracts the results of&
from|
and provides us with the elements that are uniquely presented only in one of the lists
|
运算符返回 中的元素x
,f
或两者都返回&
在操作者返回元件都x
和f
-
运算符减去&
from的结果,|
并为我们提供仅在其中一个列表中唯一呈现的元素
回答by Ahito
Input :
输入 :
x = [1,2,3,4]
f = [1,11,22,33,44,3,4]
Code:
代码:
l = list(set(x).symmetric_difference(set(f)))
print(l)
Output :
输出 :
[2, 22, 33, 11, 44]
回答by Tapaswini S
Python code to create a unique list from two lists :
从两个列表创建唯一列表的 Python 代码:
a=[1,1,2,3,5,1,8,13,6,21,34,55,89,1,2,3]
b=[1,2,3,4,5,6,7,8,9,10,11,12,2,3,4]
m=list(dict.fromkeys([a[i] for i in range(0,len(a)) if a [i] in a and a[i] in b and a[i]]))
print(m)
回答by Roma Maksimchuk
x = [1, 2, 3, 4]
f = [1, 11, 22, 33, 44, 3, 4]
list(set(x) ^ set(f))
[33, 2, 22, 11, 44]