python3中的array\u column等价于什么

2024-03-29 11:52:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个字典列表,我只想从每本字典中得到一个特定的条目。我的数据模式是:

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的替代方案是什么呢? 提前谢谢。你知道吗


Tags: 数据idsource列表fordataindex字典