怎样枚举map? | how to iterate map?

Python

m = {'a': 1, 'b': 2, 'c': 3}
for k, v in m.items():
  print '{}: {}'.format(k, v)

输出

a: 1
c: 3
b: 2

另一种写法

m = {'a': 1, 'b': 2, 'c': 3}
for k in m():
  print '{}: {}'.format(k, m[k])

Leave a Comment

Your email address will not be published.