如何从此对象在Python中返回的字典中访问特定值

问题描述 投票:-2回答:2

我有一个返回以下列表的对象:

[[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}]]

如何访问此列表中的特定值?就我而言,我需要访问在'tax_id': [[6, False, [**1**]]]

上可以找到的值“ 1”
python list
2个回答
1
投票

喜欢这个。

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]

0
投票

您可以使用各自的索引访问列表,并使用各自的键访问字典值。

以下是获得所需输出的明细:

#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])

输出:

1

将以上所有内容合并为一行(但是,较难阅读):

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

输出:

1
© www.soinside.com 2019 - 2024. All rights reserved.