【问题标题】:How to compare elements between lists of tuples?如何比较元组列表之间的元素?
【发布时间】:2020-08-15 23:43:39
【问题描述】:
new_list = [
    ('99', '99', '081589JC', 'Left'),
    ('99', '99', '051184EC', 'Right'),
    ('99', '99', '202020AZ', 'Center')
]

old_list = [
    ('081589JC', '99', '081251CG', 'Center'),
    ('99', '99', '061456JC', 'Left'),
    ('99', '99', '202020AZ', 'Above')
]

add_list = []
deL_list = []

for a in range(len(new_list)):
  if new_list[a][2] not in old_list:
    add_list.append[a]

for d in range(len(old_list)):
  if old_list[d][2] not in add_list:
    deL_list.append(d)

print(add_list)
print(deL_list)

add_list 应包含包含“081589JC”和“051184EC”的新列表元组。

deL_list 应包含包含“081251CG”和“061456JC”的 old_list 元组。

这是理想的工作方式,但是因为我们正在处理元组,所以我找不到一种方法来将一个元素与整个列表进行比较,而无需逐案处理,这对脚本没有任何帮助.当它只是一个单例元组而不是它们所代表的一整行信息时,我进行比较没有问题。任何帮助将不胜感激。

【问题讨论】:

  • 这看起来像是一套工作。您可以将这些列表转换为集合并使用difference 方法

标签: python list tuples


【解决方案1】:

如果元组内索引 2 处的值决定了您的情况下元组的唯一性,则可能值得将元组存储在字典中。 比如:

new_list = [
    ('99', '99', '081589JC', 'Left'),
    ('99', '99', '051184EC', 'Right'),
    ('99', '99', '202020AZ', 'Center')
]

old_list = [
    ('081589JC', '99', '081251CG', 'Center'),
    ('99', '99', '061456JC', 'Left'),
    ('99', '99', '202020AZ', 'Above')
]

def tuple_list_to_dict(tuple_list, key_index):
    ''' Converts a list of tuples to a dict mapping each tuples' key to the tuple '''
    return {tup[key_index]: tup for tup in tuple_list}

new_dict = tuple_list_to_dict(new_list, 2)
old_dict = tuple_list_to_dict(old_list, 2)

add_list = []
deL_list = []

# we use a set here to make sure we are looping over all the unique keys only once
for key in set(new_dict.keys() + old_dict.keys()):
    if key not in new_dict and key in old_dict:    
        deL_list.append(old_dict[key])
    elif key in new_dict and key not in old_dict:
        add_list.append(new_dict[key])

print(add_list)
print(deL_list)

【讨论】:

    【解决方案2】:

    试试这个,使用集合可以更有效地发现差异并执行in 检查。

    new_list = [
        ('99', '99', '081589JC', 'Left'),
        ('99', '99', '051184EC', 'Right'),
        ('99', '99', '202020AZ', 'Center')
    ]
    
    old_list = [
        ('081589JC', '99', '081251CG', 'Center'),
        ('99', '99', '061456JC', 'Left'),
        ('99', '99', '202020AZ', 'Above')
    ]
    
    old_keys = {it[2] for it in old_list}
    new_keys = {it[2] for it in new_list}
    added_keys = new_keys - old_keys
    deleted_keys = old_keys - new_keys
    
    add_list = [it for it in new_list if it[2] in added_keys]
    del_list = [it for it in old_list if it[2] in deleted_keys]
    
    print(add_list)
    print(del_list)
    

    【讨论】:

    • 哇!那是你如何利用这样的集合理解的天才。我自己也在尝试,但一切都错了。非常感谢!
    • 没问题。但是,如果您使用第三个键作为索引,那么您的数据结构可能会有所不同。
    【解决方案3】:

    感谢 Spencer 提供另一个可行的解决方案!

    我不得不对 for 循环进行调整,因为它不能正常工作。

    combined_set = {**new_dict, **old_dict}

    对于combined_set 中的键: 如果 key 不在 new_dict 中而 key 在 old_dict 中: deL_list.append(old_dict[key]) 在 new_dict 中的 elif 键和不在 old_dict 中的键: add_list.append(new_dict[key])

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-08
      • 1970-01-01
      • 1970-01-01
      • 2011-02-21
      • 1970-01-01
      相关资源
      最近更新 更多