简体   繁体   English

从 Python 中的字典列表中提取多个键/值对的最有效方法?

[英]Most efficient way to extract multiple key/value pairs from a list of dictionaries of dictionaries in Python?

Below is the data I'm starting with.下面是我开始的数据。 Ideally I would like to capture the 'raw' value of the 'avg', 'low', 'high' and 'numberOfAnalysts' keys from the first dictionary within this list of dictionaries, shown in bold (or captured between double asterisks (**):理想情况下,我想从该字典列表中的第一个字典中捕获“avg”、“low”、“high”和“numberOfAnalysts”键的“原始”值,以粗体显示(或在双星号之间捕获(* *):

[**{'avg': {'raw': 3.02, 'fmt': '3.02'}, 'low': {'raw': 2.5, 'fmt': '2.5'}, 'high': {'raw': 3.15, 'fmt': '3.15'}, 'yearAgoEps': {'raw': 0.91, 'fmt': '0.91'}, 'numberOfAnalysts': {'raw': 20, 'fmt': '20', 'longFmt': '20'}, 'growth': {'raw': 2.319, 'fmt': '231.90%'}}**, {'avg': {'raw': 2.62, 'fmt': '2.62'}, 'low': {'raw': 2.36, 'fmt': '2.36'}, 'high': {'raw': 3.05, 'fmt': '3.05'}, 'yearAgoEps': {'raw': 2.92, 'fmt': '2.92'}, 'numberOfAnalysts': {'raw': 20, 'fmt': '20', 'longFmt': '20'}, 'growth': {'raw': -0.103, 'fmt': '-10.30%'}}, {'avg': {'raw': 10.14, 'fmt': '10.14'}, 'low': {'raw': 8.87, 'fmt': '8.87'}, 'high': {'raw': 10.68, 'fmt': '10.68'}, 'yearAgoEps': {'raw': 8.51, 'fmt': '8.51'}, 'numberOfAnalysts': {'raw': 26, 'fmt': '26', 'longFmt': '26'}, 'growth': {'raw': 0.192, 'fmt': '19.20%'}}, {'avg': {'raw': 11.67, 'fmt': '11.67'}, 'low': {'raw': 9.39, 'fmt': '9.39'}, 'high': {'raw': 13.08, 'fmt': '13.08'}, 'yearAgoEps': {'raw': 10.14, 'fmt': '10.14'}, 'numberOfAnalysts': {'raw': 26, 'fmt': '26', 'longFmt': '26'}, 'growth': {'raw': 0.15100001, 'fmt': '15.10%'}}, {'avg': {}, 'low': {}, 'high': {}, 'yearAgoEps': {}, 'numberOfAnalysts': {}, 'growth': {}}, {'avg': {}, 'low': {}, 'high': {}, 'yearAgoEps': {}, 'numberOfAnalysts': {}, 'growth': {}}]

I've used the following code to slice out the 'avg' key/value pairs, to start:我已经使用以下代码切出“avg”键/值对,开始:

dispersion_analyst_data_final = dispersion_analyst_data_list_extract
dispersion_analyst_data_final_list_extract = [sub['avg'] for sub in dispersion_analyst_data_final]
print(str(dispersion_analyst_data_final_list_extract))

resulting in the output below:导致下面的 output:

[{'raw': 3.02, 'fmt': '3.02'}, {'raw': 2.62, 'fmt': '2.62'}, {'raw': 10.14, 'fmt': '10.14'}, {'raw': 11.67, 'fmt': '11.67'}, {}, {}]

I'm almost certain there is a more efficient way to drill down to the final 'raw' value without multiple discrete steps using sub().我几乎可以肯定,有一种更有效的方法可以深入到最终的“原始”值,而无需使用 sub() 进行多个离散步骤。 The 'raw' values of the first dictionary of the 'avg', 'low', 'high' and 'numberOfAnalysts' data will ultimately be outputted to a CSV file. “avg”、“low”、“high”和“numberOfAnalysts”数据的第一个字典的“原始”值最终将输出到 CSV 文件。 Is it possible to concatenate multiple sub() operations into the "extract" object?是否可以将多个 sub() 操作连接到“提取”object 中?

The final desired data are the 'raw' values of 'avg', 'low', 'high' and 'numberOfAnalysts' within the first dictionary entry in dispersion_analyst_data_list_extract.最终所需的数据是dispersion_analyst_data_list_extract 中第一个字典条目中的“avg”、“low”、“high”和“numberOfAnalysts”的“原始”值。 Any suggestions would be greatly appreciated.任何建议将不胜感激。

You mention-- The final desired data are the 'raw' values of 'avg', 'low', 'high' and 'numberOfAnalysts' within the first dictionary entry in dispersion_analyst_data_list_extract.您提到--最终所需的数据是dispersion_analyst_data_list_extract 中第一个字典条目中的“avg”、“low”、“high”和“numberOfAnalysts”的“原始”值。

That can be accomplished with the following.这可以通过以下方式完成。

dispersion_analyst_data_list_extract = [{'avg': {'raw': 3.02, 'fmt': '3.02'}, 'low': {'raw': 2.5, 'fmt': '2.5'}, 'high': {'raw': 3.15, 'fmt': '3.15'}, 'yearAgoEps': {'raw': 0.91, 'fmt': '0.91'}, 'numberOfAnalysts': {'raw': 20, 'fmt': '20', 'longFmt': '20'}, 'growth': {'raw': 2.319, 'fmt': '231.90%'}}, {'avg': {'raw': 2.62, 'fmt': '2.62'}, 'low': {'raw': 2.36, 'fmt': '2.36'}, 'high': {'raw': 3.05, 'fmt': '3.05'}, 'yearAgoEps': {'raw': 2.92, 'fmt': '2.92'}, 'numberOfAnalysts': {'raw': 20, 'fmt': '20', 'longFmt': '20'}, 'growth': {'raw': -0.103, 'fmt': '-10.30%'}}, {'avg': {'raw': 10.14, 'fmt': '10.14'}, 'low': {'raw': 8.87, 'fmt': '8.87'}, 'high': {'raw': 10.68, 'fmt': '10.68'}, 'yearAgoEps': {'raw': 8.51, 'fmt': '8.51'}, 'numberOfAnalysts': {'raw': 26, 'fmt': '26', 'longFmt': '26'}, 'growth': {'raw': 0.192, 'fmt': '19.20%'}}, {'avg': {'raw': 11.67, 'fmt': '11.67'}, 'low': {'raw': 9.39, 'fmt': '9.39'}, 'high': {'raw': 13.08, 'fmt': '13.08'}, 'yearAgoEps': {'raw': 10.14, 'fmt': '10.14'}, 'numberOfAnalysts': {'raw': 26, 'fmt': '26', 'longFmt': '26'}, 'growth': {'raw': 0.15100001, 'fmt': '15.10%'}}, {'avg': {}, 'low': {}, 'high': {}, 'yearAgoEps': {}, 'numberOfAnalysts': {}, 'growth': {}}, {'avg': {}, 'low': {}, 'high': {}, 'yearAgoEps': {}, 'numberOfAnalysts': {}, 'growth': {}}]

# dispersion_analyst_data_list_extract[0] is the first dictionary
# we access its desired keys using a list comprehension of the first dictionary
desired_output = [dispersion_analyst_data_list_extract[0][k] for k in ['avg', 'low', 'high',  'numberOfAnalysts']]

print(desired_output)
# Out: [{'raw': 3.02, 'fmt': '3.02'},
        {'raw': 2.5, 'fmt': '2.5'},
        {'raw': 3.15, 'fmt': '3.15'},
        {'raw': 20, 'fmt': '20', 'longFmt': '20'}]

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

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