怎樣枚舉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.