[英]Adding elements to lists inside of nested dictionary Python
對於作業,我想在這樣的嵌套字典中添加一個列表。
{Num:{Name: John, Grades:[1,2,3,4]}}
其中 Num、Name 和 Grades 是從用戶那里獲取的。 當只有一個用戶使用.update 時,我讓它工作,但是當我有兩個或更多用戶時,它不起作用,相反,它給了我類似的東西。
{Num1:{}, Num2: {Name: John, Grade:[]}}
任何幫助,將不勝感激。
我認為您可能會考慮使用list
來存儲不同的用戶。
users = []
while True:
more_user_query = input("will there be more users? [y]|n ")
if more_user_query == 'n':
break
# I think you should have get some code to get the user info
user_info = get_uesr_info()
users.append(user_info)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.