如何选择元组的第一个元素和列表中嵌套字典的值?
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)预期:
department = [[marketing, anna, Secretary], [marketing, bob, manager], [sales, claire, Secretary]]发布于 2022-03-14 15:15:27
本部分讨论的问题是:
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 =行将如下所示:
department = [[nest_list[0], x['user']['name'], x['category']] for nest_list in organization for x in nest_list[1]["values"]]发布于 2022-03-14 15:09:06
以下是我想出的:
# 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)可能会有一些改进,但这是我能够拼凑在一起的。
编辑:在我说了这句话之后,我对它进行了一些改进:
# 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)https://stackoverflow.com/questions/71469282
复制相似问题