![](/img/trans.png)
[英]How to print the key of highest value from the list's element of the dictionary?
[英]How to print every key and value from a dictionary when the value is a list?
我寫了這本字典:
dict = {
"Someone1": ['Random1', 'Random2', 'Random3'],
"Someone2": ['Random1', 'Random2', 'Random3'],
}
我想以 output 的方式打印它:
Someone1 Random1
Someone1 Random2
Someone1 Random3
Someone2 Random1
等等。
我試過這個:
for name in dict.keys():
print(name, dict[name])
但是 output 不是我想要的,我該怎么辦?
您需要另一個嵌套循環來迭代dict[name]
。 此外,您可以通過迭代鍵和值(又名items
)來簡化代碼:
for name, herbs in dict.items():
for herb in herbs:
print(name, herb)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.