首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在列表中获取元组的第一个元素和嵌套字典的值

如何在列表中获取元组的第一个元素和嵌套字典的值
EN

Stack Overflow用户
提问于 2022-03-14 14:22:16
回答 2查看 201关注 0票数 0

如何选择元组的第一个元素和列表中嵌套字典的值?

代码语言:javascript
复制
organization = [('research', {'size': 0, 'values': [], 'start': 0}), ('marketing', {'size': 2, 'values': [{'user': {'name': 'anna', 'id': 10, 'displayName': 'Anna'}, 'category': 'Secretary'}, {'user': {'name': 'bob', 'id': 11, 'displayName': 'Bobby'}, 'category': 'Manager'}], 'start': 0}), ('sales', {'size': 1, 'values': [{'user': {'name': 'claire', 'id': 13, 'displayName': 'Clarissa Claire'}, 'category': 'Secretary'}], 'start': 0})]

department = [[x['user']['name'], x['category']] for nest_list in organization for x in nest_list["values"]]
print(department)

预期:

代码语言:javascript
复制
department = [[marketing, anna, Secretary], [marketing, bob, manager], [sales, claire, Secretary]]
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-03-14 15:15:27

本部分讨论的问题是:

代码语言:javascript
复制
for nest_list in organization for x in nest_list["values"]

nest_list是一个元组(organization中的所有元组),您希望x遍历values列表中的所有对象(这是每个元组的第二个成员),但是直接跳到values。最后应该是nest_list[1]["values"]--在进入values之前,您需要进入第二个元组元素。

然后,在列表定义中,还需要将nest_list[0]作为每个元素列表的第一个元素,以便您可以根据需要拥有部门。因此,department =行将如下所示:

代码语言:javascript
复制
department = [[nest_list[0], x['user']['name'], x['category']] for nest_list in organization for x in nest_list[1]["values"]]
票数 0
EN

Stack Overflow用户

发布于 2022-03-14 15:09:06

以下是我想出的:

代码语言:javascript
复制
# Define your list
organization = [('research', {'size': 0, 'values': [], 'start': 0}), ('marketing', {'size': 2, 'values': [{'user': {'name': 'anna', 'id': 10, 'displayName': 'Anna'}, 'category': 'Secretary'}, {'user': {'name': 'bob', 'id': 11, 'displayName': 'Bobby'}, 'category': 'Manager'}], 'start': 0}), ('sales', {'size': 1, 'values': [{'user': {'name': 'claire', 'id': 13, 'displayName': 'Clarissa Claire'}, 'category': 'Secretary'}], 'start': 0})]

# Create empty lists to be used by the loop
employee = []
employee_list = []

# Loop through each department
for counter in range(0,len(organization)):
    # Check if there are any entries by looking at the size key
    if organization[counter][1]['size'] > 0:
        # loop through each user
        for user in range(0,len(organization[counter][1]['values'])):
            # for each user, pull out the Name and Category, and add it to the employee list
            employee = [organization[counter][0],organization[counter][1]['values'][user]['user']['name'],organization[counter][1]['values'][user]['category']]
            # Add each employee as a nested list in the Employee list
            employee_list.append(employee)

print(employee_list)

可能会有一些改进,但这是我能够拼凑在一起的。

编辑:在我说了这句话之后,我对它进行了一些改进:

代码语言:javascript
复制
# Define your list
organization = [('research', {'size': 0, 'values': [], 'start': 0}), ('marketing', {'size': 2, 'values': [{'user': {'name': 'anna', 'id': 10, 'displayName': 'Anna'}, 'category': 'Secretary'}, {'user': {'name': 'bob', 'id': 11, 'displayName': 'Bobby'}, 'category': 'Manager'}], 'start': 0}), ('sales', {'size': 1, 'values': [{'user': {'name': 'claire', 'id': 13, 'displayName': 'Clarissa Claire'}, 'category': 'Secretary'}], 'start': 0})]

# Create empty lists to be used by the loop
employee = []
employee_list = []

# Loop through each department
for counter in range(0,len(organization)):
    # Loop through each user based on the size key
    for user in range(0,organization[counter][1]['size']):
        # for each user, pull out the Name and Category, and add it to the employee list
        employee = [organization[counter][0],organization[counter][1]['values'][user]['user']['name'],organization[counter][1]['values'][user]['category']]
        # Add each employee as a nested list in the Employee list
        employee_list.append(employee)

print(employee_list)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71469282

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档