亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

通過某個鍵合并字典列表

通過某個鍵合并字典列表

慕娘9325324 2023-04-18 15:09:52
我有一個由相同結構list組成的,dictsample = [{'a':1, 'b':2, 'c':3}, {'a':1, 'b':2, 'c':4}, {'a':2, 'b':2, 'c':5}, {'a':2, 'b':3, 'c':5}]我想按鍵組合它們a,輸出應該是[{'a': 1, 'd': [{'b':2, 'c':3}, {'b':2, 'c':4}]}, {'a': 2, 'd': [{'b':2, 'c':5}, {'b': 3, 'c':5}]}]
查看完整描述

5 回答

?
繁星coding

TA貢獻1797條經驗 獲得超4個贊

您可以使用itertools.groupby

>>> from itertools import groupby

>>> result = []

>>> for key, group in groupby(sorted(sample, key=lambda x:x['a']), key=lambda x:x.pop('a')):

? ? ? ? result.append({'a':key, 'd':[*group]})

>>> result

[{'a': 1, 'd': [{'b': 2, 'c': 3}, {'b': 2, 'c': 4}]},

?{'a': 2, 'd': [{'b': 2, 'c': 5}, {'b': 3, 'c': 5}]}]

sorted注意:如果保證字典列表按 key 的值排序,則不需要a。


查看完整回答
反對 回復 2023-04-18
?
開滿天機

TA貢獻1786條經驗 獲得超13個贊

按鍵組合:


dict_list = [{'a':1, 'b':2, 'c':3}, {'a':1, 'b':2, 'c':4}, {'a':2, 'b':2, 'c':5}, {'a':2, 'b':3, 'c':5}]

new_dict = {}


for d in dict_list:

    a = d.pop('a', None)

    if new_dict.get(a):

         new_dict[a].append(d)

    else:

        new_dict[a] = [d]

轉換為列表:


final_list = [{'a': key, 'd': value} for key, value in new_dict.items()]

print(final_list)

[{'a': 1, 'd': [{'c': 3, 'b': 2}, {'c': 4, 'b': 2}]}, {'a': 2, 'd': [{'c': 5, 'b': 2}, {'c': 5, 'b': 3}]}]



查看完整回答
反對 回復 2023-04-18
?
互換的青春

TA貢獻1797條經驗 獲得超6個贊

sample = [{'a':1, 'b':2, 'c':3}, {'a':1, 'b':2, 'c':4}, {'a':2, 'b':2, 'c':5}, {'a':2, 'b':3, 'c':5}]



tmp = {}

for v in sample:

    tmp.setdefault(v['a'], []).append(v)

    del v['a']


out = [{'a': k, 'd': v} for k, v in tmp.items()]


from pprint import pprint

pprint(out)

印刷:


[{'a': 1, 'd': [{'b': 2, 'c': 3}, {'b': 2, 'c': 4}]},

 {'a': 2, 'd': [{'b': 2, 'c': 5}, {'b': 3, 'c': 5}]}]


查看完整回答
反對 回復 2023-04-18
?
倚天杖

TA貢獻1828條經驗 獲得超3個贊

不幸的是,這可能是一個有點曲折的代碼,但它有效:


from itertools import groupby


sample = [{'a':1, 'b':2, 'c':3},

          {'a':1, 'b':2, 'c':4},

          {'a':2, 'b':2, 'c':5},

          {'a':2, 'b':3, 'c':5}]


main_key = "a"


print(

    [{main_key:k,

      "d": [{kk: vv for kk, vv in dct.items() if kk != main_key}

            for dct in v]}

     for k, v in groupby(sample, lambda d:d[main_key])]

)

給出:


[{'a': 1, 'd': [{'b': 2, 'c': 3}, {'b': 2, 'c': 4}]},

 {'a': 2, 'd': [{'b': 2, 'c': 5}, {'b': 3, 'c': 5}]}]

(為了便于閱讀,輸出稍微漂亮一點)


查看完整回答
反對 回復 2023-04-18
?
繁花不似錦

TA貢獻1851條經驗 獲得超4個贊

使用 Pandas 進行查詢的替代解決方案。


import pandas as pd

sample = [{'a':1, 'b':2, 'c':3}, {'a':1, 'b':2, 'c':4}, {'a':2, 'b':2, 'c':5}, {'a':2, 'b':3, 'c':5}]


df=pd.DataFrame(sample)

這將使用上面的示例列表創建一個 DataFrame df。下一步是遍歷 GroupBy 對象并根據需要創建輸出。


final_list=[]

for i, temp_df in df.groupby('a'):

    temp_list=[]

    for j in temp_df.index:

        temp_list.append({'b':temp_df.loc[:,'b'][j],'c':temp_df.loc[:,'c'][j]})

    final_list.append({'a':temp_df.loc[:,'a'][j],'d':temp_list})


查看完整回答
反對 回復 2023-04-18
  • 5 回答
  • 0 關注
  • 190 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號