我試圖將串列轉換為元組到自定義串列到字典。我可以劃分管理員、包所有者、提交者、消費者和只讀。請檢查下面的代碼和輸出
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'}]
}
}
但如果任何角色詳細資訊記錄未在 lst 中退出,我將嘗試傳遞一個空串列。請檢查預期輸出
預期輸出:
{'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 的新手,請用邏輯建議我。謝謝你
uj5u.com熱心網友回復:
您還可以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})
uj5u.com熱心網友回復:
by_role是 a defaultdict(list),因此如果您嘗試訪問by_role['admin']or ,您將得到一個空串列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': []
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/473180.html
上一篇:GoLangpostgrestestcontainer將BindMounts轉換為Mounts
下一篇:將字典轉換為資料框
