在列表列表中查找和更新重复项
问题内容:
我正在寻找一种解决以下问题的Python方法。我有(我认为是)有效的解决方案,但它具有复杂的流程控制,而且不是“漂亮”的。(基本上是C ++解决方案)
我有一个清单清单。每个列表包含多个不同类型的项目(每个列表可能有10个项目)。列表的总体顺序无关紧要,但是任何单个列表中项目的顺序都很重要。(即我无法更改)。
我希望通过在单个列表的末尾添加一个额外的字段来“标记”重复项。但是,在这种情况下,“重复”列表是在几个预选字段中具有相等值的列表,但并非所有字段(没有“
true”重复)。
例如:如果这是来自5项列表的原始数据,并且重复项被定义为在第一字段和第三字段中具有相等的值:
['apple', 'window', 'pear', 2, 1.55, 'banana']
['apple', 'orange', 'kiwi', 3, 1.80, 'banana']
['apple', 'envelope', 'star_fruit', 2, 1.55, 'banana']
['apple', 'orange', 'pear', 2, 0.80, 'coffee_cup']
['apple', 'orange', 'pear', 2, 3.80, 'coffee_cup']
第一,第四和第五个列表将是重复的,因此所有列表应按以下方式更新:
['apple', 'window', 'pear', 2, 1.55, 'banana', 1]
['apple', 'orange', 'kiwi', 3, 1.55, 'banana', 0]
['apple', 'envelope', 'star_fruit', 2, 1.55,'banana', 0]
['apple', 'orange', 'pear', 2, 3.80, 'coffee_cup', 2]
['apple', 'orange', 'pear', 2, 3.80, 'coffee_cup', 3]
感谢您的帮助或指导。我认为这可能超出了《学习Python》一书。
问题答案:
from collections import defaultdict
lists = [['apple', 'window', 'pear', 2, 1.55, 'banana'],
['apple', 'orange', 'kiwi', 3, 1.80, 'banana'],
['apple', 'envelope', 'star_fruit', 2, 1.55, 'banana'],
['apple', 'orange', 'pear', 2, 0.80, 'coffee_cup'],
['apple', 'orange', 'pear', 2, 3.80, 'coffee_cup']]
dic = defaultdict(int)
fts = []
for lst in lists:
first_third = lst[0], lst[2]
dic[first_third] += 1
if dic[first_third] == 2: fts.append(first_third)
lst.append(dic[first_third])
for lst in lists:
if (lst[0], lst[2]) not in fts:
lst[-1] -= 1
print(lists)
编辑:谢谢utdemir。first_third = lst[0], lst[2]
是正确的,不是first_third = lst[0] + lst[2]
Edit2:为清楚起见,更改了变量名称。
Edit3:进行了更改,以反映原始海报的真正需求以及他的更新列表。不再需要,只是进行了所需的更改。