提问人:thothbk 提问时间:2/8/2023 最后编辑:Jamesthothbk 更新时间:2/8/2023 访问量:59
我想从 3 个 python 列表中构建一个嵌套词典列表,其中 3 个列表中有一个包含重复项
I want to construct a list of nested dictionaries from 3 python lists where one of the 3 lists contains duplicates
问:
我正在尝试构建一个嵌套词典列表,是的,List_A中有重复的值。
基本上,对于每个外部键,我都在尝试合并其所有内部键/值对
我写了以下代码行:
A = ['sibsp', 'sibsp', 'pclass', 'pclass', 'pclass', 'age', 'age', 'age', 'age', 'age', 'age', 'fare', 'fare', 'fare', 'fare', 'fare', 'parch', 'parch']
B = ['(-0.001, 1.0]', '(1.0, 8.0]', '1', '2', '3', '(0.419, 19.0]', '(19.0, 25.0]', '(25.0, 31.8]', '(31.8, 41.0]', '(41.0, 80.0]', 'nan', '(-0.001, 7.854]', '(10.5, 21.679]', '(21.679, 39.688]', '(39.688, 512.329]', '(7.854, 10.5]', '(-0.001, 1.0]', '(1.0, 6.0]']
C = [-0.043281487422643504, 0.5199640685633579, -1.0039159555090957, -0.3644848445981264, 0.6664826567146916, -0.4000843004236305, 0.24183838283179523, -0.04150528802138758, -0.2219732761660194, 0.04505675173309271, 0.4037823142739484, 0.8047930720327325, -0.16862729546072658, -0.25014415313271576, -1.0575407967677335, 0.9062269696875864, 0.04021888607232296, -0.32565170564086077]
listOfNestedDicts=[ {i:{j:k}} for (i,j,k) in zip(List_A,List_B,List_C) ]
我得到了以下输出,这是一个嵌套字典列表,但这不是我想要的,它在我的应用程序中不起作用,因为 sibsb、pclass、age、fever 和 parch 是数据帧中的唯一列,我将把内部 B 键映射到它们相应的 C 值
[{'sibsp': {'(-0.001, 1.0]': -0.043281487422643504}},
{'sibsp': {'(1.0, 8.0]': 0.5199640685633579}},
{'pclass': {'1': -1.0039159555090957}},
{'pclass': {'2': -0.3644848445981264}},
{'pclass': {'3': 0.6664826567146916}},
{'age': {'(0.419, 19.0]': -0.4000843004236305}},
{'age': {'(19.0, 25.0]': 0.24183838283179523}},
{'age': {'(25.0, 31.8]': -0.04150528802138758}},
{'age': {'(31.8, 41.0]': -0.2219732761660194}},
{'age': {'(41.0, 80.0]': 0.04505675173309271}},
{'age': {'nan': 0.4037823142739484}},
{'fare': {'(-0.001, 7.854]': 0.8047930720327325}},
{'fare': {'(10.5, 21.679]': -0.16862729546072658}},
{'fare': {'(21.679, 39.688]': -0.25014415313271576}},
{'fare': {'(39.688, 512.329]': -1.0575407967677335}},
{'fare': {'(7.854, 10.5]': 0.9062269696875864}},
{'parch': {'(-0.001, 1.0]': 0.04021888607232296}},
{'parch': {'(1.0, 6.0]': -0.32565170564086077}}]
我想得到的输出如下:
[{'sibsp': {'(-0.001, 1.0]': -0.043281487422643504,'(1.0, 8.0]': 0.5199640685633579}},
{'pclass': {'1': -1.0039159555090957 ,'2': -0.3644848445981264, '3': 0.6664826567146916}},
{'age': {'(0.419, 19.0]': -0.4000843004236305,'(19.0, 25.0]': 0.24183838283179523,'(25.0, 31.8]': -0.04150528802138758,'(31.8, 41.0]': -0.2219732761660194,'(41.0, 80.0]': 0.04505675173309271,'nan': 0.4037823142739484}},
{'fare': {'(-0.001, 7.854]': 0.8047930720327325,'(10.5, 21.679]': -0.16862729546072658, '(21.679, 39.688]': -0.25014415313271576,'(39.688, 512.329]': -1.0575407967677335,'(7.854, 10.5]': 0.9062269696875864}},
{'parch': {'(-0.001, 1.0]': 0.04021888607232296,'(1.0, 6.0]': -0.32565170564086077}}]
答:
0赞
Pedro Rocha
2/8/2023
#1
我使用以下代码得到了所需的结果:
result = []
keys = list(set(A))
for key in keys:
inner_dict = {}
for i in range(len(A)):
if A[i] == key:
inner_dict[B[i]] = C[i]
result.append({key: inner_dict})
0赞
C Hecht
2/8/2023
#2
既然你使用的是 a,你也可以通过 pandas 的路线来达到预期的结果,在一个结合了列表和字写理解的单行中:pandas.DataFrame
import pandas as pd
result = {key: [value2.to_dict() for key2, value2 in sub_df.iloc[:, 1:].iterrows()] for key, sub_df in pd.DataFrame([A, B, C], index=['a', 'b', 'c']).T.groupby('a')}
这个想法是:我们在 -statement 中按 A 列分组,然后遍历子 DataFrame 的行,并将它们转换为字典。groupby
评论