如何比较列表/集合在python中的列表? [英] How to compare a list of lists/sets in python?
问题描述
比较2个列表/集合和输出差异的最简单方法是什么?是否有任何内置函数可以帮助我比较嵌套列表/集?
输入:
First_list = [['Test.doc,'1a1a1a',1111],
['Test2.doc,'2b2b2b',2222],
['Test3.doc, '3c3c3c',3333]
]
Secnd_list = [['test.doc','1a1a1a',1111],
['Test2.doc','2b2b2b',2222]
['Test3.doc','8p8p8p',9999],
['Test4.doc','4d4d4d',4444]]
预期输出:
Differences = [['Test3.doc ','3c3c3c',3333],
['Test3.doc','8p8p8p',9999],
['Test4.doc','4d4d4d',4444]]
解决方案所以你想要两个项目列表之间的区别。
first_list = [['Test.doc','1a1a1a',1111],
['Test2.doc','2b2b2b' ,2222],
['Test3.doc','3c3c3c',3333]]
secnd_list = [['Test.doc','1a1a1a',1111],
['Test2 .doc','2b2b2b',2222],
['Test3.doc','8p8p8p',9999],
['Test4.doc','4d4d4d',4444]]
首先,我将每个列表的列表转换为元组列表,因此元组是哈希表(列表不是),所以你可以将你的元组列表转换为一组元组:
first_tuple_list = [tuple(lst)for lst in first_list ]
secnd_tuple_list = [tstable(lst)for lst in secnd_list]
set:
first_set = set(first_tuple_list)
secnd_set = set(secnd_tuple_list)
EDIT(由sdolan建议):你可以在一行中为每个列表完成最后两个步骤:
first_set = set(map(tuple,first_list))
secnd_set = set(map(tuple,secnd_list))
注意:
map
是一个函数式编程命令,第一个参数(在这种情况下是tuple
函数)到第二个参数中的每个项目(在我们的例子中是一个列表列表)。
并找到集合之间的对称差异:
>> first_set.symmetric_difference(secnd_set)
set([('Test3.doc','3c3c3c',3333),
('Test3.doc','8p8p8p',9999),
'test4.doc','4d4d4d',4444)])
注意
first_set ^ secnd_set
等效于symmetric_difference
。
使用集合(例如,使用python 2.2),它相当直接。例如,使用列表推导:
>>> [x'in first_list if x not in secnd_list] + [x for x in secnd_list if x not in first_list]
[['Test3.doc','3c3c3c',3333],
[ Test3.doc','8p8p8p',9999],
['Test4.doc','4d4d4d',4444]]
或使用函数
filter
命令和lambda
函数。 (您必须测试两种方式并结合使用)。>>过滤器(lambda x:x不在secnd_list,first_list)+过滤器(lambda x:x不在first_list,secnd_list)
[['Test3.doc','3c3c3c',3333] $ b ['Test3.doc','8p8p8p',9999],
['Test4.doc','4d4d4d',4444]]
What is the easiest way to compare the 2 lists/sets and output the differences? Are there any built in functions that will help me compare nested lists/sets?
Inputs:
First_list = [['Test.doc, '1a1a1a', 1111], ['Test2.doc, '2b2b2b', 2222], ['Test3.doc, '3c3c3c', 3333] ] Secnd_list = [['Test.doc', '1a1a1a', 1111], ['Test2.doc', '2b2b2b', 2222], ['Test3.doc', '8p8p8p', 9999], ['Test4.doc', '4d4d4d', 4444]]
Expected Output:
Differences = [['Test3.doc', '3c3c3c', 3333], ['Test3.doc', '8p8p8p', 9999], ['Test4.doc', '4d4d4d', 4444]]
解决方案So you want the difference between two lists of items.
first_list = [['Test.doc', '1a1a1a', 1111], ['Test2.doc', '2b2b2b', 2222], ['Test3.doc', '3c3c3c', 3333]] secnd_list = [['Test.doc', '1a1a1a', 1111], ['Test2.doc', '2b2b2b', 2222], ['Test3.doc', '8p8p8p', 9999], ['Test4.doc', '4d4d4d', 4444]]
First I'd turn each list of lists into a list of tuples, so as tuples are hashable (lists are not) so you can convert your list of tuples into a set of tuples:
first_tuple_list = [tuple(lst) for lst in first_list] secnd_tuple_list = [tuple(lst) for lst in secnd_list]
Then you can make sets:
first_set = set(first_tuple_list) secnd_set = set(secnd_tuple_list)
EDIT (suggested by sdolan): You could have done the last two steps for each list in a one-liner:
first_set = set(map(tuple, first_list)) secnd_set = set(map(tuple, secnd_list))
Note:
map
is a functional programming command that applies the function in the first argument (in this case thetuple
function) to each item in the second argument (which in our case is a list of lists).and find the symmetric difference between the sets:
>>> first_set.symmetric_difference(secnd_set) set([('Test3.doc', '3c3c3c', 3333), ('Test3.doc', '8p8p8p', 9999), ('Test4.doc', '4d4d4d', 4444)])
Note
first_set ^ secnd_set
is equivalent tosymmetric_difference
.Also if you don't want to use sets (e.g., using python 2.2), its quite straightforward to do. E.g., with list comprehensions:
>>> [x for x in first_list if x not in secnd_list] + [x for x in secnd_list if x not in first_list] [['Test3.doc', '3c3c3c', 3333], ['Test3.doc', '8p8p8p', 9999], ['Test4.doc', '4d4d4d', 4444]]
or with the functional
filter
command andlambda
functions. (You have to test both ways and combine).>>> filter(lambda x: x not in secnd_list, first_list) + filter(lambda x: x not in first_list, secnd_list) [['Test3.doc', '3c3c3c', 3333], ['Test3.doc', '8p8p8p', 9999], ['Test4.doc', '4d4d4d', 4444]]
这篇关于如何比较列表/集合在python中的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!