简体   繁体   English

如何从 Python 中的 object 返回的字典中访问特定值

[英]How can I access a specific value from the dictionary returned by this object in Python

I have an object that returns the following list:我有一个返回以下列表的 object:

[[0, 'virtual_94', 
    {'sequence': 10, 
    'display_type': False, 
    'product_uom_qty': 1, 
    'qty_delivered_manual': 0, 
    'price_unit': 1000, 
    'discount': 0, 
    'customer_lead': 0, 
    'product_id': 1, 
    'product_no_variant_attribute_value_ids': [[6, False, []]], 
    'name': 'Produto de teste', 
    'product_uom': 1, 
    'analytic_tag_ids': [[6, False, []]], 
    'route_id': False, 
    *'tax_id': [[6, False, [**1**]]],* 
    'sale_line_exemption_id': False}]]

How can I access a specific value on this list?如何访问此列表中的特定值? In my case, I need to access the value "1" that can be found on 'tax_id': [[6, False, [**1**]]]就我而言,我需要访问可以在'tax_id': [[6, False, [**1**]]]

Like this.像这样。

infos = [[0, 'virtual_94', {'sequence': 10, 'display_type': False, 'product_uom_qty': 1, 'qty_delivered_manual': 0, 'price_unit': 1000, 'discount': 0, 'customer_lead': 0, 'product_id': 1, 'product_no_variant_attribute_value_ids': [[6, False, []]], 'name': 'Produto de teste', 'product_uom': 1, 'analytic_tag_ids': [[6, False, []]], 'route_id': False, 'tax_id': [[6, False, [1]]], 'sale_line_exemption_id': False}]]

ID = infos[0][2]["tax_id"][0][2][0]

You can access lists with their respective indexes and dictionary values with their respective keys.您可以使用它们各自的索引访问列表,并使用它们各自的键访问字典值。

Here's a breakdown to get to your desired output:这是获得所需 output 的细分:

#Access the third element (index 2) in your initial list to get a dictionary
d = your_list[2]

#Get value for key 'tax_id' : [[6, False, [**1**]]]
tax_id_list = d['tax_id']

# Get sublist: [6, False, [**1**]]
tax_id_sub_list = tax_id_list[0]

# Get list with one in it: [**1**]
element_with_one = tax_id_sub_list[2]

#Finally, access the first element in that list: 1
print(element_with_one[0])

Outputs:输出:

1

Merging all the above into a single line (however, harder to read):将以上所有内容合并到一行中(但是,更难阅读):

print(your_list[0][2]['tax_id'][0][2][0])

Outputs:输出:

1

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

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