3 回答
TA贡献1786条经验 获得超13个赞
你可以简单地使用一个列表来达到这个目的
dblist = []
dblist.append( {"id": 1, "fn": "JM", "ln" : "Cruz", "dob": "October 5, 1980"})
dblist.append( {"id": 2, "fn": "JD", "ln" : "Castillo", "dob": "August 18, 1979"})
dblist.append({"id": 3, "fn": "Maria", "ln" : "Torres", "dob": "August 3, 1992"})
for db in dblist:
print("ID: " + str(db["id"]))
print("Full Name: " + db["fn"] + " " + db["ln"])
print("Birthday: " + db["dob"])
print("----------------------")
输出
ID: 1
Full Name: JM Cruz
Birthday: October 5, 1980
----------------------
ID: 2
Full Name: JD Castillo
Birthday: August 18, 1979
----------------------
ID: 3
Full Name: Maria Torres
Birthday: August 3, 1992
----------------------
TA贡献1909条经验 获得超7个赞
您只能创建一个dict
,并且密钥是用户 ID。其他信息如“fn, ln, dob”可能在列表中。您将按特定顺序附加这 3 个信息,以便您可以从列表中检索任何必要的信息。
样本:
db = {"1" : [fn1, ln1, dob1], "2": [fn2, ln2, dob2]}
TA贡献1804条经验 获得超7个赞
db = [{"id": 1, "fn": "JM", "ln" : "Cruz", "dob": "October 5, 1980"}, {"id": 2, "fn": "JD", "ln" : "Castillo", "dob": "August 18, 1979"}, {"id": 3, "fn": "Maria", "ln" : "Torres", "dob": "August 3, 1992"}]
for i in db:
print(f"ID: {i['id']}\nFull Name: {i['fn']} {i['ln']}\nBirthday: {i['dob']}\n{'-' * 22}")
或者你可以“玩”拆包:
for i in db:
print("ID: {}\nFull Name: {} {}\nBirthday: {}\n".format(*i.values()) + "-" * 22)
添加回答
举报