社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

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

pmatos • 5 年前 • 506 次点击  

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

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

如何访问此列表中的特定值?在我的例子中,我需要访问值“1”,可以在 'tax_id': [[6, False, [**1**]]]

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/48820
 
506 次点击  
文章 [ 2 ]  |  最新文章 5 年前
Rithin Chalumuri
Reply   •   1 楼
Rithin Chalumuri    5 年前

您可以访问具有各自索引的列表和具有各自键的字典值。

这里有一个细目以达到您想要的输出:

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

输出:

Florian Bernard
Reply   •   2 楼
Florian Bernard    5 年前

这样地。

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]