Sco*_*ott -2 python dictionary
我想在Python中循环这个字典,并打印键和值,我是Python的新手,互联网上的例子不起作用或打印错误.
那么我怎样才能正确循环下表并显示键/值?
letters = {
"m":1, "n":1, "r":1, "M":1, "N":1, "R":1, "l":1, "L":1, "1":1,
"3":1, "6":1, "7":1, "8":1, "0":1,
}
Run Code Online (Sandbox Code Playgroud)
ern*_*nie 10
你可以使用items(),例如:
for k, v in letters.items():
print k,v
Run Code Online (Sandbox Code Playgroud)
或者因为它是字典,迭代键,例如:
# uses iterator, as opposed to returning all the keys via .keys()
for k in letters:
print k, letters[k]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
395 次 |
| 最近记录: |