[英]Python compare words and remove duplicates in two list of lists
I have the following lists:我有以下列表:
list_1_test = [['hi','there','how'],['we','are','one']]
list_2_test = [['hi','you','how'],['we','not','one']]
I wish to compare the words in the list and get the following output:我希望比较列表中的单词并得到以下 output:
list_3_test = [['there','you'],['are','not']]
I know how to do this in a simple list, for example:我知道如何在一个简单的列表中做到这一点,例如:
list_1_test = ['hi','there','how']
list_2_test = ['hi','you','how']
list_3_test=[]
for i in list_1_test:
if i not in list_2_test:
list_3_test.append(i)
for i in list_2_test:
if i not in list_1_test:
list_3_test.append(i)
and the result is结果是
['there', 'you']
But when it comes to list of lists, my brain is fried.但是当谈到列表列表时,我的大脑被炸了。 The order matters.
顺序很重要。 Any help is much appreciated.
任何帮助深表感谢。
If order doesn't matter, you can use set operations + list comprehension:如果顺序无关紧要,您可以使用集合操作 + 列表理解:
out = [list(set(l1).union(l2) - set(l1).intersection(l2)) for l1, l2 in zip(list_1_test, list_2_test)]
Output: Output:
[['you', 'there'], ['are', 'not']]
If order matters, you can use dict.fromkeys
:如果顺序很重要,您可以使用
dict.fromkeys
:
out = []
for l1, l2 in zip(list_1_test, list_2_test):
one = dict.fromkeys(l1).keys()
two = dict.fromkeys(l2).keys()
out.append(list(one - two) + list(two - one))
Output: Output:
[['there', 'you'], ['are', 'not']]
You can use the sets and symetric_difference method (^):您可以使用集合和 symetric_difference 方法 (^):
list3 = []
for l1, l2 in zip(list_1_test, list_2_test):
sym_dif = set(l1)^set(l2)
list3.append(list(sym_dif))
Previous code in list comprehension format:列表理解格式的先前代码:
list3 = [set(l1)^set(l2) for l1, l2 in zip(list_1_test, list_2_test)]
all_list = []
for i in list_1_test:
all_list.append(' '.join(i))
for i in list_2_test:
all_list.append(' '.join(i))
list_str = ' '.join(all_list)
result = []
for i in list_str.split():
if list_str.count(i) == 1:
result.append(i)
print(result)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.