在python中比较两个列表,仅保留匹配项和不匹配项

时间:2019-02-13 15:46:24

标签: python python-3.x

我正在尝试遍历两个元组列表,并正在寻找匹配项。我想生成两个元组列表作为输出:一个包含匹配项(两个列表中第一个值都出现的元组)和一个不匹配(一个值在一个列表中但另一个不出现的元组)。

我尝试通过在嵌套循环中遍历两个列表并比较那里的元组来实现此目的。但是,当我这样做时,结果“不匹配”列表也包含确实匹配的元组,并且也出现在“匹配”列表中。

我正在努力避免这种情况。必须有更好的方法来做到这一点。任何帮助将不胜感激。

下面是一个示例:

data1 = [
    ("Aaden", "3"),
    ("Aamir", "3"),
    ("Aarav", "3"),
    ("aaren", "3"),
    ("aarika", "3"),
    ("Adad", "3")
]

data2 = [
    ("Aaden", "3"),
    ("Aamir", "3"),
    ("Aarav", "3"),
    ("aaren", "3"),
    ("aarika", "3"),
    ("Aaron", "3"),
    ("Abaddon", "3"),
    ("abagael", "3"),
    ("abagail", "3"),
    ("Abatu", "3"),
    ("abbe", "3"),
    ("abbey", "3"),
    ("abbi", "3"),
    ("abbie", "3"),
    ("Abbot", "3"),
    ("Abbott", "3"),
    ("abby", "3"),
    ("abbye", "3"),
    ("Abdel", "3"),
    ("Abdiel", "3"),
    ("Abdul", "3"),
    ("Abdulkarim", "3"),
    ("Abdullah", "3"),
    ("Abduxuel", "3"),
    ("Abe", "3"),
    ("Abel", "3"),
    ("Abelard", "1"),
    ("abigael", "3"),
    ("abigail", "3"),
    ("abigale", "3"),
    ("Abigar", "3"),
    ("Abigor", "3"),
    ("Abner", "4"),
    ("abra", "3"),
    ("Abraham", "3"),
    ("Abram", "3"),
    ("Acacia", "3"),
    ("Ace", "3"),
    ("Achilles", "3"),
    ("Aclahayr", "3"),
    ("ada", "6"),
    ("awdad", "3"),
    ("awad", "3"),
    ("Ainiond", "3"),
    ("Adww", "3"),
    ("james","3")
]

match = []
no_match = []

for item1 in data1:
    for item2 in data2:
        if item1[0] == item2[0]:
            match.append(item1)
        else:
            no_match.append(item1)      

print('First List Count: {}'.format(len(data1)))
print('Second List Count: {}'.format(len(data2)))

print('Match Count: {}'.format(len(match)))
print('No Match Count: {}'.format(len(no_match)))

输出:

First List Count: 6
Second List Count: 46
Match Count: 5
No Match Count: 271

4 个答案:

答案 0 :(得分:3)

您可能要为此使用 sets 。 您可以找到两个集合的交集(&的公共元组:

match = set(data1) & set(data2)

您可以获得具有对称差异或等效的^的非公共元素:

no_match = len(set(data1) ^ set(data2))

附加链接中的sets — Unordered collections of unique elements的更多信息。

答案 1 :(得分:0)

有:将两个列表放入集合中,并使用集合操作。例如

s1 = set(list1)
s2 = set(list2)
match = s1 & s2

答案 2 :(得分:0)

这可以使用Set来完成:

data1 = set(data1)
data2 = set(data2)
match = data1.intersection(data2)
no_match = data1.union(data2) - data1.intersection(data2)
print('match \n', match)
print('Length of Match: ', len(data1.intersection(data2)))
print('no match \n', no_match)
print('Lenght of no match: ', len(data1.union(data2) - data1.intersection(data2)))

输出:

no match
 {('Aarav', '3'), ('Aaden', '3'), ('Aamir', '3'), ('aaren', '3'), ('aarika', '3')}
Length of Match:  5
no match
 {('Abdulkarim', '3'), ('Aaron', '3'), ('Abbot', '3'), ('abbye', '3'), ('Adww', '3'), ('Aclahayr', '3'), ('Achilles', '3'), ('abagail', '3'), ('awad', '3'), ('Abbott', '3'), ('Acacia', '3'), ('Ace', '3'), ('Adad', '3'), ('Abdel', '3'), ('Abigar', '3'), ('Abdiel', '3'), ('abby', '3'), ('Abdullah', '3'), ('abbie', '3'), ('abra', '3'), ('awdad', '3'), ('Ainiond', '3'), ('Abigor', '3'), ('Abatu', '3'), ('abbey', '3'), ('Abner', '4'), ('Abduxuel', '3'), ('abbi', '3'), ('james', '3'), ('abagael', '3'), ('abbe', '3'), ('Abraham', '3'), ('Abdul', '3'), ('abigael', '3'), ('abigale', '3'), ('Abel', '3'), ('Abe', '3'), ('Abram', '3'), ('abigail', '3'), ('Abelard', '1'), ('ada', '6'), ('Abaddon', '3')}
Lenght of no match:  42

答案 3 :(得分:0)

与第二个列表相比,第一个列表中的匹配项和不匹配项

match_items1=[]
no_match_items1=[]

for temp_data1 in data1:
    for temp_data2 in data2:
        if temp_data2[0].strip() == temp_data1[0].strip():
            match_items1.append(temp_data1)
            break
    else:
        no_match_items1.append(temp_data1)

output: For Match items [('Aaden', '3'), ('Aamir', '3'), ('Aarav', '3'), ('aaren', '3'), ('aarika', '3')]
        For Non-Match items [('Adad', '3')] 

第二列表中与第一列表相比较的匹配项和非匹配项

match_items2=[]
no_match_items2=[]

for temp_data2 in data2:
    for temp_data1 in data1:
        if temp_data1[0].strip() == temp_data2[0].strip():
            match_items2.append(temp_data2)
            break
    else:
        no_match_items2.append(temp_data2)

output: For Match items [('Aaden', '3'), ('Aamir', '3'), ('Aarav', '3'), ('aaren', '3'), ('aarika', '3')]
        For Non-Match items [('Aaron', '3'), ('Abaddon', '3'), ('abagael', '3'), ('abagail', '3'), ('Abatu', '3'), ('abbe', '3'), ('abbey', '3'), ('abbi', '3'), ('abbie', '3'), ('Abbot', '3'), ('Abbott', '3'), ('abby', '3'), ('abbye', '3'), ('Abdel', '3'), ('Abdiel', '3'), ('Abdul', '3'), ('Abdulkarim', '3'), ('Abdullah', '3'), ('Abduxuel', '3'), ('Abe', '3'), ('Abel', '3'), ('Abelard', '1'), ('abigael', '3'), ('abigail', '3'), ('abigale', '3'), ('Abigar', '3'), ('Abigor', '3'), ('Abner', '4'), ('abra', '3'), ('Abraham', '3'), ('Abram', '3'), ('Acacia', '3'), ('Ace', '3'), ('Achilles', '3'), ('Aclahayr', '3'), ('ada', '6'), ('awdad', '3'), ('awad', '3'), ('Ainiond', '3'), ('Adww', '3'), ('james', '3')]