Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python中比较两个以上的列表?_Python - Fatal编程技术网

如何在Python中比较两个以上的列表?

如何在Python中比较两个以上的列表?,python,Python,我不熟悉Python,也不熟悉Stackoverflow,有谁能告诉我比较两个以上列表的有效(pythonic)方法吗?我想登记所有3个列表的所有元素,并以这样的方式显示,用户将能够知道哪些元素出现在所有3个列表中,哪些元素出现在列表1中,而不是列表2中,或者哪些元素是重复的。我已经使用嵌套循环进行了比较 List1 = [10,10,11,12,15,16,18,19] List2 = [10,11,13,15,16,19,20] List3 = [10,11,11,12,15,19,

我不熟悉Python,也不熟悉Stackoverflow,有谁能告诉我比较两个以上列表的有效(pythonic)方法吗?我想登记所有3个列表的所有元素,并以这样的方式显示,用户将能够知道哪些元素出现在所有3个列表中,哪些元素出现在列表1中,而不是列表2中,或者哪些元素是重复的。我已经使用嵌套循环进行了比较

List1 = [10,10,11,12,15,16,18,19]  
List2 = [10,11,13,15,16,19,20]  
List3 = [10,11,11,12,15,19,21,23] 

# Checking whether List 1 value present in List2 and List 3
for l1 in List1:  
    if l1 in List2: 
        List2.remove(l1)  
        if l1 in List3: 
            List3.remove(l1)  
            print(l1," ",l1," ",l1)  
        else:  
            print(l1," ",l1," ","NA")  
    else:
        if l1 in List3:   
            List3.remove(l1)
            print(l1," ","NA"," ",l1)
        else:
            print(l1," ","NA"," ","NA")

# Checking whether List 2 value present in List3
for l2 in List2:
    if l2 in List3:
        List3.remove(l2)
        print("NA"," ",l2," ",l2)
    else:
        print("NA"," ",l2," ","NA")

# Checking for values present only in List 3

for l3 in List3:
    print("NA","NA",l3)

--- Output---
List1 List2 List3
10   10   10
10   NA   NA
11   11   11
12   NA   12
15   15   15
16   16   NA
18   NA   NA
19   19   19
NA   13   NA
NA   20   NA
NA NA 11
NA NA 21
NA NA 23
NA   20   NA
NA NA 11
NA NA 21
NA NA 23

有没有更好的方法来比较这些列表?

List1=[10,10,11,12,15,16,18,19]
列表2=[10,11,13,15,16,19,20]
清单3=[10,11,11,12,15,19,21,23]

>>> set(List1).intersection(set(List2))
set([16, 19, 10, 11, 15])
>>> set(List1).intersection(set(List3))
set([19, 10, 11, 12, 15])
>>> set(List2).intersection(set(List3))
set([19, 10, 11, 15])
>>> 
交集生成两个集合中的数字。集合包含列表中的不同值,但不重复


你的问题我不清楚你是否想考虑重复,如果是这样的话。

< P>如果你想要所有列表的交集,把一个集合变成一个集合,然后把其他的传递给<代码>交叉点< /代码>:

List1 = [10,10,11,12,15,16,18,19]
List2 = [10,11,13,15,16,19,20]
List3 = [10,11,11,12,15,19,21,23]

print(set(List1).intersection(List2, List3))
这给了你:

set([19, 10, 11, 15])
要从列表3中获取唯一元素,请执行以下操作:

List1 = [10,10,11,12,15,16,18,19]
List2 = [10,11,13,15,16,19,20]
List3 = [10,11,11,12,15,19,21,23]

inter = set(List1).intersection(List2, List3)

diff3 = set(List3).difference(inter)

print(diff3)
set([12, 21, 23])
如果要仅保留每个列表中不在交叉点的元素,请执行以下操作:

List1 = [10,10,11,12,15,16,18,19]
List2 = [10,11,13,15,16,19,20]
List3 = [10,11,11,12,15,19,21,23]

inter = set(List1).intersection(List2, List3)

List1[:] = [ele for ele in List1 if ele not in inter]
List2[:] = [ele for ele in List2 if ele not in inter]
List3[:] = [ele for ele in List3 if ele not in inter]

print(List1)
print(List2)
print(List3)
这将给你:

[12, 16, 18]
[13, 16, 20]
[12, 21, 23]
使用Pandas可以比较多个列表,而空列将自动填充为NaN

Pandas是python的数据可视化库

安装熊猫:pip安装熊猫

编辑:

主要链接:


你到底想做什么?如果这是关于检查哪些列表中的值可以找到,你可以使用字典,例如,我想一个更具python风格的方法是set。然后用“相交”和“差异”进行比较,你想要达到什么目标?比较的预期结果是什么?顺便说一句,从正向迭代的列表中删除项目通常是不安全的。这有点像锯下你坐在上面的树枝。一种方法是创建一个新的列表,复制要保留的项目。但正如其他人所说,您可能可以更有效地使用集合来做您想做的事情。但是,您确实需要更清楚地解释您实际想要实现的目标。您希望从该样本数据中得到什么结果?对不起!如果你不明白我的问题。我只想比较3个列表数据并将其写入文件,我想登记3个列表的所有元素,并以这样的方式显示,用户将能够知道哪一个元素出现在所有3个列表中,或者哪一个元素出现在列表1中但不在列表2中,或者哪一个元素是重复的@Schore你能解释一下,使用集合是如何实现的吗?
import pandas as pd
d = {'List1' : pd.Series(List1),'List2' : pd.Series(List2),'List3': pd.Series(List3)}

df = pd.DataFrame(d)

print(df)

***OUTPUT:***
      List1  List2  List3
      10      NaN     NaN
      11      11     11
      12      NaN     12
      15      15     15
      16      16     NaN
      18      NaN    NaN
      19      19     19
      NaN     13     NaN