我有一个字典列表,我只想从每本字典中得到一个特定的条目。我的数据模式是:
data = [
{
"_id": "uuid",
"_index": "my_index",
"_score": 1,
"_source": {
"id" : 1,
"price": 100
}
},
{
"_id": "uuid",
"_index": "my_index",
"_score": 1,
"_source": {
"id" : 2,
"price": 150
}
},
{
"_id": "uuid",
"_index": "my_index",
"_score": 1,
"_source": {
"id" : 3,
"price": 90
}
}
]
我想要的输出:
formatted_data = [
{
"id": 1,
"price": 100
},
{
"id": 2,
"price": 150
},
{
"id": 3,
"price": 90
}
]
为了格式化数据,我使用了迭代(for
),比如
formatted_data = []
for item in data:
formatted_data.append(item['_source'])
在PHP中,我可以使用^{for
循环。那么在我的例子中,python3中for
的替代方案是什么呢?
提前谢谢。你知道吗
您可以使用列表理解:
相关问题 更多 >
编程相关推荐