尝试自定义 python 嵌套词典
trying to customize the python nested dictionaries
我正在尝试将列表转换为元组,将自定义列表转换为字典。我能够划分管理员、包所有者、提交者、消费者和只读。请检查下面的代码和输出
from collections import defaultdict
role_details = ['po','sub', 'cons', 'admin','read']
lst = [('name1', 'email1', 'psid1', 'new1', 11, '1', 'po'),
('name2', 'email2', 'psid2', 'new2', 12, '2', 'sub'),
('name3', 'email3', 'psid3', 'new3', 13, '3', 'sub'),
('name4', 'email4', 'psid4', 'new4', 14, '4', 'po'),
('name5', 'email5', 'psid5', 'new5', 15, '5', 'cons')]
by_role = defaultdict(list)
for name, email, psid, new, id1, id2, role_name in lst:
by_role[role_name].append({"name": name, "email": email, "psid": psid})
print({"add_sub": dict(by_role)})
输出:
{'add_sub':
{'po': [{'name': 'name1', 'email': 'email1', 'psid': 'psid1'}, {'name': 'name4', 'email': 'email4', 'psid': 'psid4'}],
'sub': [{'name': 'name2', 'email': 'email2', 'psid': 'psid2'}, {'name': 'name3', 'email': 'email3', 'psid': 'psid3'}],
'cons': [{'name': 'name5', 'email': 'email5', 'psid': 'psid5'}]
}
}
但如果任何角色详细信息记录在第一个列表中未退出,我将尝试传递一个空列表。请检查预期输出
预期输出:
{'add_sub':
{'admin': [],
'po': [{'name': 'name1', 'email': 'email1', 'psid': 'psid1'}, {'name': 'name4', 'email': 'email4', 'psid': 'psid4'}],
'read': [],
'sub': [{'name': 'name2', 'email': 'email2', 'psid': 'psid2'}, {'name': 'name3', 'email': 'email3', 'psid': 'psid3'}],
'cons': [{'name': 'name5', 'email': 'email5', 'psid': 'psid5'}]
}
}
我是 python 的新手,请给我一些逻辑建议。谢谢
by_role
是一个 defaultdict(list)
,因此如果您尝试访问 by_role['admin']
或 by_role['read only']
.
,您将得到一个空列表
事实上,您需要做的就是尝试访问这些键一次,然后将它们添加到 defaultdict
,因此您可以遍历 role_details
并执行此操作:
for name, email, psid, new, id1, id2, role_name in lst:
by_role[role_name].append({"name": name, "email": email, "psid": psid})
for role_name in role_details:
_ = by_role[role_name] # Try to access every key, and don't do anything with it.
然后,您应该得到预期的输出:
{'add_sub': {
'pack owner': [
{'name': 'name1', 'email': 'email1', 'psid': 'psid1'},
{'name': 'name4', 'email': 'email4', 'psid': 'psid4'}
],
'submitter': [
{'name': 'name2', 'email': 'email2', 'psid': 'psid2'},
{'name': 'name3', 'email': 'email3', 'psid': 'psid3'}
],
'consumer': [{'name': 'name5', 'email': 'email5', 'psid': 'psid5'}],
'admin': [],
'read only': []
}
}
您还可以预先使用所需的所有密钥创建 dict
。在这种情况下,您甚至不需要 defaultdict
,除非您以后需要它的功能。
#from collections import defaultdict
role_details = ['pack owner','submitter', 'consumer', 'admin','read only']
lst = [('name1', 'email1', 'psid1', 'new1', 11, '1', 'pack owner'),
('name2', 'email2', 'psid2', 'new2', 12, '2', 'submitter'),
('name3', 'email3', 'psid3', 'new3', 13, '3', 'submitter'),
('name4', 'email4', 'psid4', 'new4', 14, '4', 'pack owner'),
('name5', 'email5', 'psid5', 'new5', 15, '5', 'consumer')]
#by_role = defaultdict(list)
by_role = {k: [] for k in role_details}
for name, email, psid, new, id1, id2, role_name in lst:
by_role[role_name].append({"name": name, "email": email, "psid": psid})
print({"add_sub": by_role})
{'add_sub':
{'pack owner': [{'name': 'name1', 'email': 'email1', 'psid': 'psid1'}, {'name': 'name4', 'email': 'email4', 'psid': 'psid4'}],
'submitter': [{'name': 'name2', 'email': 'email2', 'psid': 'psid2'}, {'name': 'name3', 'email': 'email3', 'psid': 'psid3'}],
'consumer': [{'name': 'name5', 'email': 'email5', 'psid': 'psid5'}],
'admin': [],
'read only': []}
}
另请注意,您不需要在打印中调用 dict()
,因为 by_role
已经是 dict
.
如果您确实需要defaultdict
,您可以用这一行做同样的事情:
from collections import defaultdict
# ...code...
by_role = defaultdict(list, {k: [] for k in role_details})
我正在尝试将列表转换为元组,将自定义列表转换为字典。我能够划分管理员、包所有者、提交者、消费者和只读。请检查下面的代码和输出
from collections import defaultdict
role_details = ['po','sub', 'cons', 'admin','read']
lst = [('name1', 'email1', 'psid1', 'new1', 11, '1', 'po'),
('name2', 'email2', 'psid2', 'new2', 12, '2', 'sub'),
('name3', 'email3', 'psid3', 'new3', 13, '3', 'sub'),
('name4', 'email4', 'psid4', 'new4', 14, '4', 'po'),
('name5', 'email5', 'psid5', 'new5', 15, '5', 'cons')]
by_role = defaultdict(list)
for name, email, psid, new, id1, id2, role_name in lst:
by_role[role_name].append({"name": name, "email": email, "psid": psid})
print({"add_sub": dict(by_role)})
输出:
{'add_sub':
{'po': [{'name': 'name1', 'email': 'email1', 'psid': 'psid1'}, {'name': 'name4', 'email': 'email4', 'psid': 'psid4'}],
'sub': [{'name': 'name2', 'email': 'email2', 'psid': 'psid2'}, {'name': 'name3', 'email': 'email3', 'psid': 'psid3'}],
'cons': [{'name': 'name5', 'email': 'email5', 'psid': 'psid5'}]
}
}
但如果任何角色详细信息记录在第一个列表中未退出,我将尝试传递一个空列表。请检查预期输出
预期输出:
{'add_sub':
{'admin': [],
'po': [{'name': 'name1', 'email': 'email1', 'psid': 'psid1'}, {'name': 'name4', 'email': 'email4', 'psid': 'psid4'}],
'read': [],
'sub': [{'name': 'name2', 'email': 'email2', 'psid': 'psid2'}, {'name': 'name3', 'email': 'email3', 'psid': 'psid3'}],
'cons': [{'name': 'name5', 'email': 'email5', 'psid': 'psid5'}]
}
}
我是 python 的新手,请给我一些逻辑建议。谢谢
by_role
是一个 defaultdict(list)
,因此如果您尝试访问 by_role['admin']
或 by_role['read only']
.
事实上,您需要做的就是尝试访问这些键一次,然后将它们添加到 defaultdict
,因此您可以遍历 role_details
并执行此操作:
for name, email, psid, new, id1, id2, role_name in lst:
by_role[role_name].append({"name": name, "email": email, "psid": psid})
for role_name in role_details:
_ = by_role[role_name] # Try to access every key, and don't do anything with it.
然后,您应该得到预期的输出:
{'add_sub': {
'pack owner': [
{'name': 'name1', 'email': 'email1', 'psid': 'psid1'},
{'name': 'name4', 'email': 'email4', 'psid': 'psid4'}
],
'submitter': [
{'name': 'name2', 'email': 'email2', 'psid': 'psid2'},
{'name': 'name3', 'email': 'email3', 'psid': 'psid3'}
],
'consumer': [{'name': 'name5', 'email': 'email5', 'psid': 'psid5'}],
'admin': [],
'read only': []
}
}
您还可以预先使用所需的所有密钥创建 dict
。在这种情况下,您甚至不需要 defaultdict
,除非您以后需要它的功能。
#from collections import defaultdict
role_details = ['pack owner','submitter', 'consumer', 'admin','read only']
lst = [('name1', 'email1', 'psid1', 'new1', 11, '1', 'pack owner'),
('name2', 'email2', 'psid2', 'new2', 12, '2', 'submitter'),
('name3', 'email3', 'psid3', 'new3', 13, '3', 'submitter'),
('name4', 'email4', 'psid4', 'new4', 14, '4', 'pack owner'),
('name5', 'email5', 'psid5', 'new5', 15, '5', 'consumer')]
#by_role = defaultdict(list)
by_role = {k: [] for k in role_details}
for name, email, psid, new, id1, id2, role_name in lst:
by_role[role_name].append({"name": name, "email": email, "psid": psid})
print({"add_sub": by_role})
{'add_sub':
{'pack owner': [{'name': 'name1', 'email': 'email1', 'psid': 'psid1'}, {'name': 'name4', 'email': 'email4', 'psid': 'psid4'}],
'submitter': [{'name': 'name2', 'email': 'email2', 'psid': 'psid2'}, {'name': 'name3', 'email': 'email3', 'psid': 'psid3'}],
'consumer': [{'name': 'name5', 'email': 'email5', 'psid': 'psid5'}],
'admin': [],
'read only': []}
}
另请注意,您不需要在打印中调用 dict()
,因为 by_role
已经是 dict
.
如果您确实需要defaultdict
,您可以用这一行做同样的事情:
from collections import defaultdict
# ...code...
by_role = defaultdict(list, {k: [] for k in role_details})