[英]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.