繁体   English   中英

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

[英]Group the list of dictionary as list of values based on key

以下是字典列表,

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

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

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

理解每个字典只有两个键,您将需要遍历每个字典并追加到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']}

您可以使用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]}]

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

干杯。

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]}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM