将字典列表分组为基于键的值列表

时间:2018-12-24 07:26:03

标签: python python-3.x list dictionary group-by

以下是字典列表,

 [{'12': 'carrom', 'name': 'tom'}, 
 {'7': 'tennis', 'name': 'tom'}, 
 {'5': 'cycling', 'name': 'tom'},
 {'9': 'tennis', 'name': 'sam'}]

如何以以下格式构建列表理解?

{'tom' : [12,7,5], 'sam' : [9]} 

3 个答案:

答案 0 :(得分:2)

在了解每个词典只有两个键的情况下,您将需要遍历每个词典并追加到defaultdict

from collections import defaultdict

d = defaultdict(list)
for l in lst:
    # Pop the name key, so we're only left with the other key.
    name_key = l.pop('name')
    # Extract the remaining key from `l`.
    other_key = list(l)[0] 
    d[name_key].append(other_key)

print(d)
# defaultdict(list, {'sam': ['9'], 'tom': ['12', '7', '5']})

请注意,这会破坏性地遍历您的词典。要将d当作普通命令,请使用

d = dict(d)

由于defaultdictdict的子类。


另一个选择是熊猫(因为有了图书馆):

df = pd.DataFrame(lst).set_index('name')
df
          12        5       7       9
name                                 
tom   carrom      NaN     NaN     NaN
tom      NaN      NaN  tennis     NaN
tom      NaN  cycling     NaN     NaN
sam      NaN      NaN     NaN  tennis

df.notna().dot(df.columns).groupby(level=0).agg(list).to_dict()
# {'sam': ['9'], 'tom': ['12', '7', '5']}

答案 1 :(得分:1)

您可以使用itertools.groupby首先将字典列表分组,

from itertools import groupby
groupby_list = [list(g) for k, g in groupby(alist, key=lambda x: x['name'])]

这将输出一个列表,

[[{'12': 'carrom', 'name': 'tom'},
  {'7': 'tennis', 'name': 'tom'},
  {'5': 'cycling', 'name': 'tom'}],
[{'9': 'tennis', 'name': 'sam'}]]

然后,您必须获取每个嵌套列表的键,并使用isdigit()方法过滤字符串键。我将它组合成一个很长的理解表达式,有点复杂。

[{group[0]['name'] : [int(number) for number in list(set().union(*(d.keys() for d in list(group)))) if number.isdigit()]} for group in groupby_list]

结果就是您想要的:

[{'tom': [12, 7, 5]}, {'sam': [9]}]

希望这个答案会有所帮助。

干杯。

答案 2 :(得分:1)

your_list_name = [i['name'] for i in your_list]
your_list_name 
    ['tom', 'tom', 'tom', 'sam']

your_list_keys = [i.keys() for i in your_list]
your_list_digit_keys = [[item for item in sublist  if item.isdigit()==True] for sublist in your_list_keys]
your_list_digit_keys = [item for sublist in your_list_digit_keys for item in sublist]
your_list_digit_keys = list(map(int, your_list_digit_keys))
your_list_digit_keys
    [12, 7, 5, 9]

my_dict={} # Initializing the dictionary
for i in range(len(your_list_name)):
    key = your_list_name[i]
    if key in my_dict:
        my_dict[key] += [your_list_digit_keys[i]]
    else:
        my_dict[key] = [your_list_digit_keys[i]]

my_dict
    {'sam': [9], 'tom': [12, 7, 5]}
相关问题