我有一个如下所示的列表:
dict_list = [{'angle': 5.0,
'basic_info': '111',
'x': [1,2,3,4],
'y': [3,2,1,4],},
{'angle': 25.0,
'basic_info': '111',
'x': [1,2,3,4],
'y': [3,1,5,2],},
{'angle': 3.0,
'basic_info': '111',
'x': [1,2,3,4],
'y': [1,1,4,1],},]
我想得到 dict 角度 25,我该怎么做?
更新:
与 Pandas 玩了一段时间后,我发现这可能是可能的
df = pd.DataFrame(dict_list)
temp = df.query("(angle ==25 )").T.to_dict()[temp.keys()[0]]
temp
返回
{'angle': 25.0, 'basic_info': '111', 'x': [1, 2, 3, 4], 'y': [3, 1, 5, 2]}
但这有点黑客。
最佳答案
假设字典中的每个角度都是唯一的,并且每个字典都包含键“角度”:
df = None
for sub_dict in dict_list:
if sub_dict['angle'] == 25:
df = pd.DataFrame({'x': sub_dict['x'], 'y': sub_dict['y']})
break # Stops after finding the first matching angle.
if df is not None:
df.plot(x='x', y='y')
关于Python 和 Pandas : Find a dict within a list according to a key's value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38908903/