【问题标题】:Comparing Python nested lists and count duplicates [duplicate]比较 Python 嵌套列表并计算重复项 [重复]
【发布时间】:2012-09-25 17:55:06
【问题描述】:

我有两个带有字符串的嵌套列表(list_alist_b),详情如下:

list_a = [
('shop1', 'stand1', 'shelf1', 'fruit1'),
('shop1', 'stand1', 'shelf2', 'fruit2'),
('shop1', 'stand1', 'shelf3', 'fruit3'),
('shop1', 'stand2', 'shelf1', 'fruit1'),
('shop1', 'stand2', 'shelf2', 'fruit2'),
('shop1', 'stand2', 'shelf3', 'fruit3'),
('shop2', 'stand3', 'shelf1', 'fruit1'),
('shop2', 'stand3', 'shelf2', 'fruit2'),
('shop2', 'stand3', 'shelf3', 'fruit3')
]
list_b = [
('shop1', 'stand1', 'shelf1', 'fruit1'),
('shop1', 'stand1', 'shelf2', 'fruit2'),
('shop1', 'stand1', 'shelf2', 'fruit2'),
('shop1', 'stand1', 'shelf3', 'fruit3'),
('shop1', 'stand1', 'shelf3', 'fruit3'),
('shop1', 'stand1', 'shelf3', 'fruit3'),
('shop1', 'stand2', 'shelf1', 'fruit1'),
('shop1', 'stand2', 'shelf1', 'fruit1'),
('shop1', 'stand2', 'shelf1', 'fruit1'),
('shop1', 'stand2', 'shelf2', 'fruit2'),
('shop1', 'stand2', 'shelf2', 'fruit2'),
('shop1', 'stand2', 'shelf2', 'fruit2'),
('shop1', 'stand2', 'shelf3', 'fruit3'),
('shop2', 'stand3', 'shelf1', 'fruit1'),
('shop2', 'stand3', 'shelf1', 'fruit1'),
('shop2', 'stand3', 'shelf2', 'fruit2'),
('shop2', 'stand3', 'shelf3', 'fruit3'),
('shop2', 'stand3', 'shelf3', 'fruit3'),
('shop2', 'stand3', 'shelf3', 'fruit3')
]

我想在list_a 中从list_b 中找到相同的行,计算“重复”行并将list_a 与一个附加列(出现次数)合并为一个新列表,如下所示:

result_list = [
('shop1', 'stand1', 'shelf1', 'fruit1', 1),
('shop1', 'stand1', 'shelf2', 'fruit2', 2),
('shop1', 'stand1', 'shelf3', 'fruit3', 3),
('shop1', 'stand2', 'shelf1', 'fruit1', 3),
('shop1', 'stand2', 'shelf2', 'fruit2', 3),
('shop1', 'stand2', 'shelf3', 'fruit3', 1),
('shop2', 'stand3', 'shelf1', 'fruit1', 2),
('shop2', 'stand3', 'shelf2', 'fruit2', 1),
('shop2', 'stand3', 'shelf3', 'fruit3', 3)
]

有没有快速有效的方法来做到这一点?

【问题讨论】:

标签: python list count nested duplicates


【解决方案1】:
dict_a = {row: 0 for row in list_a}
for row in list_b:
    if row in dict_a:
        dict_a[row] += 1

result = [row + (dict_a[row],) for row in list_a]

在 Python 2.6 上使用 dict((row, 0) for row in list_a) 而不是字典解析。

【讨论】:

  • 工作很漂亮,但我忘了提及我的 Python 版本,它是 2.6,所以我对其进行了一些更改。非常感谢!
【解决方案2】:

使用Counter():

    >>> from collections import Counter
    >>> count=Counter(list_b)
    >>> [list(x)+[count[x]] for x in list_a]

    [['shop1', 'stand1', 'shelf1', 'fruit1', 1], 
    ['shop1', 'stand1', 'shelf2', 'fruit2', 2],
    ['shop1', 'stand1', 'shelf3', 'fruit3', 3],
    ['shop1', 'stand2', 'shelf1', 'fruit1', 3],
    ['shop1', 'stand2', 'shelf2', 'fruit2', 3],
    ['shop1', 'stand2', 'shelf3', 'fruit3', 1],
    ['shop2', 'stand3', 'shelf1', 'fruit1', 2], 
    ['shop2', 'stand3', 'shelf2', 'fruit2', 1], 
    ['shop2', 'stand3', 'shelf3', 'fruit3', 3]]`

【讨论】:

  • 工作很漂亮,但我忘了提及我的 Python 版本,它是 2.6,所以我对其进行了一些更改。非常感谢!
  • 你可以在 pythom 2.6 中使用defaultdict
【解决方案3】:

这些不是嵌套列表,而是元组。这实际上是你的储蓄。请参阅 Most Efficient way to calculate Frequency of values in a Python list? ,它几乎可以立即工作。要获取重复项,请取两个字典的keys(),并计算它们的差异。

【讨论】:

  • 从您的链接中可以很好地工作。非常感谢!
猜你喜欢
  • 2018-09-19
  • 2017-10-06
  • 1970-01-01
  • 1970-01-01
  • 2014-12-16
  • 1970-01-01
  • 2015-06-07
  • 1970-01-01
  • 2016-11-12
相关资源
最近更新 更多