代码之家  ›  专栏  ›  技术社区  ›  iNoob

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

  •  0
  • iNoob  · 技术社区  · 5 年前

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

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

    我尽量避免这样。一定有更好的办法。任何帮助都将不胜感激。

    下面是一个例子:

    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
    
    3 回复  |  直到 5 年前
        1
  •  3
  •   yatu Sayali Sonawane    5 年前

    你可能想用 集合 为此。 你可以找到有交集的公共元组( & )两组中:

    match = set(data1) & set(data2)
    

    你可以用对称差分或等价的方法得到非公共元素 ^ :

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

    更多关于 sets — Unordered collections of unique elements 在连接的链接中。

        2
  •  0
  •   blue_note    5 年前

    有:将两个列表组合成一组,并使用集合操作。如

    s1 = set(list1)
    s2 = set(list2)
    match = s1 & s2
    
        3
  •  0
  •   heena bawa    5 年前

    这可以使用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