2016-02-28 67 views
-2

如果第二个列表比较重要,在两个列表比较之后是否有任何方法检测区分?Python中列表之间的区别

List1 items: 1 2 3 4 
List2 items: 1 2 4 

预期的结果应该是:remove 3

List1 items: 1 2 3 4 
List2 items: 1 2 3 4 5 

预期的结果应该是:no further changes

List1 items: 1 2 3 4 5 
List2 items: 1 2 3 4 6 

预期的结果应该是:remove 5

+0

你有没有尝试过任何东西? – jonrsharpe

+0

当然,我试着相交和类似的东西:[x for platform_normal_array如果x不在s] – DzLL

+0

请编辑问题给[mcve]。 – jonrsharpe

回答

0

“我只是想删除1列表中的项目,这是不存在的第二个列表”

“预期的结果应该是:除去3”

In [11]: list1 = {1, 2, 3, 4} 
In [12]: list2 = {1, 2, 4} 
In [13]: set.difference(list1, list2) 
Out[13]: {3} 

“预期的结果应该是:无进一步的变化”

In [14]: list1 = {1, 2, 3, 4} 
In [15]: list2 = {1, 2, 3, 4, 5} 
In [16]: set.difference(list1, list2) 
Out[16]: set() 

“预期的结果应该是:除去5”

In [17]: list1 = {1, 2, 3, 4, 5} 
In [18]: list2 = {1, 2, 3, 4, 6} 
In [19]: set.difference(list1, list2) 
Out[19]: {5} 
1

set(list1) - set(list2)将返回一组缺少的项目。

相关问题