【问题标题】:Merging repeated items in a list into a python dictionary将列表中的重复项合并到python字典中
【发布时间】:2013-12-01 20:54:36
【问题描述】:

我有一个看起来像下面这样的列表,其中相同的项目重复了几次。

l = (['aaron distilled ', 'alcohol', '5'], 
['aaron distilled ', 'gin', '2'], 
['aaron distilled ', 'beer', '6'], 
['aaron distilled ', 'vodka', '9'], 
['aaron evicted ', 'owner', '1'], 
['aaron evicted ', 'bum', '1'], 
['aaron evicted ', 'deadbeat', '1'])

我想将其转换为字典列表,在其中我会将第一项的所有重复项合并到一个键中,因此最终结果如下所示:

data = {'aaron distilled' :  ['alcohol', '5', 'gin', '2',  'beer', '6', 'vodka', '9'], 
'aaron evicted ':  ['owner', '1', 'bum', '1', 'deadbeat', '1']}

我正在尝试类似:

result = {}
for row in data:
    key = row[0]
    result = {row[0]: row[1:] for row in data}

for dicts in data:
   for key, value in dicts.items():
    new_dict.setdefault(key,[]).extend(value)

但我得到了错误的结果。我对 python 非常陌生,非常感谢有关如何解决此问题的任何提示或参考在哪里可以找到允许我执行此操作的信息。谢谢!

【问题讨论】:

    标签: python list python-3.x dictionary


    【解决方案1】:

    使用collections.defaultdict() object 方便:

    from collections import defaultdict
    
    result = defaultdict(list)
    
    for key, *values in data:
        result[key].extend(values)
    

    您的第一次尝试将覆盖键; dict 理解不会合并这些值。第二次尝试似乎将data 列表中的列表视为字典,因此根本行不通。

    演示:

    >>> from collections import defaultdict
    >>> data = (['aaron distilled ', 'alcohol', '5'], 
    ... ['aaron distilled ', 'gin', '2'], 
    ... ['aaron distilled ', 'beer', '6'], 
    ... ['aaron distilled ', 'vodka', '9'], 
    ... ['aaron evicted ', 'owner', '1'], 
    ... ['aaron evicted ', 'bum', '1'], 
    ... ['aaron evicted ', 'deadbeat', '1'])
    >>> result = defaultdict(list)
    >>> for key, *values in data:
    ...    result[key].extend(values)
    ... 
    >>> result
    defaultdict(<class 'list'>, {'aaron distilled ': ['alcohol', '5', 'gin', '2', 'beer', '6', 'vodka', '9'], 'aaron evicted ': ['owner', '1', 'bum', '1', 'deadbeat', '1']})
    

    【讨论】:

    • 或者因为它是 Python3,for k, *v in data: result[k] += v
    • @gnibbler:也不错,我会用它。 :-)
    • 如果要避免defaultdict,也可以在for循环中使用result.setdefault(key, []).extend(values)
    • @MartijnPieters 非常感谢您的帮助。我喜欢这个解决方案,但它给我带来了一个新问题,因为结果更难排序。有没有办法避免defaultdict 并获得变成元组并使其可排序的结果?当我尝试将 defauldict 对象转换为元组时,它创建的就像嵌套字典一样难以排序。非常感谢您的帮助和时间。
    • @user2962024: defaultdict() 只是 dict 的一个子类,具有一个额外的功能;我不确定你想在这里排序。
    【解决方案2】:

    如果 L 中的项目按第一个元素排序,您可以使用groupby

    >>> L = (['aaron distilled ', 'alcohol', '5'], 
    ... ['aaron distilled ', 'gin', '2'], 
    ... ['aaron distilled ', 'beer', '6'], 
    ... ['aaron distilled ', 'vodka', '9'], 
    ... ['aaron evicted ', 'owner', '1'], 
    ... ['aaron evicted ', 'bum', '1'], 
    ... ['aaron evicted ', 'deadbeat', '1'])
    >>> from operator import itemgetter
    >>> from itertools import groupby
    >>> {k: [j for a,b,c in g for j in b,c] for k, g in groupby(L, itemgetter(0))}
    {'aaron evicted ': ['owner', '1', 'bum', '1', 'deadbeat', '1'], 'aaron distilled ': ['alcohol', '5', 'gin', '2', 'beer', '6', 'vodka', '9']}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-06
      • 2016-08-14
      • 2020-05-30
      • 2017-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多