如何访问字典的键和值并迭代 for 循环?
dictionary = {1: "one", 2: "two", 3: "three"}
我的输出将是这样的:
1 one
2 two
3 three
您可以使用此代码片段。
dictionary = {1:"a", 2:"b", 3:"c"}
# To iterate over the keys
for key in dictionary.keys(): # or `for key in dictionary`
print(key)
# To iterate over the values
for value in dictionary.values():
print(value)
# To iterate both the keys and values
for key, value in dictionary.items():
print(key, '\t', value)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)