2023-11-08|閱讀時間 ‧ 約 2 分鐘

Python list of dictionary

簡單介紹 如何在list中放入dictionary

candidates=[
{"Name":"Lai","Party":"DPP","Born":"New Taipei" },
{"Name":"Hou","Party":"KMT","Born":"Chiayi"},
{"Name":"Ko","Party":"TPP","Born":"Hsinchu"},
{"Name":"Guo","Party":None,"Born":"New Taipei"}
]
for i in candidates:
print(i["Name"])

terminal 印出
Lai
Hou
Ko
Guo​
--------------------------------------------------------
candidates=[
{"Name":"Lai","Party":"DPP","Born":"New Taipei" },
{"Name":"Hou","Party":"KMT","Born":"Chiayi"},
{"Name":"Ko","Party":"TPP","Born":"Hsinchu"},
{"Name":"Guo","Party":None,"Born":"New Taipei"}
]
for i in candidates:
print(i["Name"],i["Party"],i["Born"],sep=", ")

terminal 印出
Lai, DPP, New Taipei
Hou, KMT, Chiayi
Ko, TPP, Hsinchu
Guo, None, New Taipei




分享至
成為作者繼續創作的動力吧!
© 2024 vocus All rights reserved.