繁体   English   中英

如何访问嵌套 Python 字典中的特定值?

[英]How do I access a specific value in a nested Python dictionary?

我试图弄清楚如何过滤状态为“awaiting_delivery”的字典。 我不确定如何做到这一点(或者如果不可能)。 我是 python 和编程的新手。 我在 Ubuntu 20.04 上的 VS 代码上使用 Python 3.8.5。 下面的数据是我创建的示例数据,类似于来自 API 的 json 数据。 关于如何过滤“状态”的任何帮助都会很棒。 谢谢你。

nested_dict = {
    'list_data': [
        {
            'id': 189530,
            'total': 40.05,
            'user_data': {
                'id': 1001,
                'first_name': 'jane',
                'last_name': 'doe'
            },
            'status': 'future_delivery'
        },
        {
            'id': 286524,
            'total': 264.89,
            'user_data': {
                'id': 1002,
                'first_name': 'john',
                'last_name': 'doe'
            },
            'status': 'awaiting_delivery'
        },
        {
            'id': 368725,
            'total': 1054.98,
            'user_data': {
                'id': 1003,
                'first_name': 'chris',
                'last_name': 'nobody'
            },
            'status': 'awaiting_delivery'
        },
        {
            'id': 422955,
            'total': 4892.78,
            'user_data': {
                'id': 1004,
                'first_name': 'mary',
                'last_name': 'madeup'
            },
            'status': 'future_delivery'
        }
    ],
    'current_page': 1,
    'total': 2,
    'first': 1,
    'last': 5,
    'per_page': 20
}

#confirm that nested_dict is a dictionary
print(type(nested_dict))

#create a list(int_list) from the nested_dict dictionary
int_list = nested_dict['list_data']

#confirm that int_list is a list
print(type(int_list))

#create the int_dict dictionary from the int_list list
for int_dict in int_list:
    print(int_dict)

#this is my attempt at filtering the int_dict dictionar for all orders with a status of awaiting_delivery
for order in int_dict:
    int_dict.get('status')
    print(order)

来自终端的 Output 如下:

<class 'dict'>
<class 'list'>
{'id': 189530, 'total': 40.05, 'user_data': {'id': 1001, 'first_name': 'jane', 'last_name': 'doe'}, 'status': 'future_delivery'}
{'id': 286524, 'total': 264.89, 'user_data': {'id': 1002, 'first_name': 'john', 'last_name': 'doe'}, 'status': 'awaiting_delivery'}
{'id': 368725, 'total': 1054.98, 'user_data': {'id': 1003, 'first_name': 'chris', 'last_name': 'nobody'}, 'status': 'awaiting_delivery'}
{'id': 422955, 'total': 4892.78, 'user_data': {'id': 1004, 'first_name': 'mary', 'last_name': 'madeup'}, 'status': 'future_delivery'}
id
total
user_data
status

您可以通过对 dicts 列表执行条件列表理解来获得过滤后的 dicts 列表:

# filter the data
list_data_filtered = [entry for entry in nested_dict['list_data']
                      if entry['status'] == 'awaiting_delivery']

# print out the results
for entry in list_data_filtered:
    print(entry)

# results
# {'id': 286524, 'total': 264.89, 'user_data': {'id': 1002, 'first_name': 'john', 'last_name': 'doe'}, 'status': 'awaiting_delivery'}
# {'id': 368725, 'total': 1054.98, 'user_data': {'id': 1003, 'first_name': 'chris', 'last_name': 'nobody'}, 'status': 'awaiting_delivery'}

暂无
暂无

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

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