Python 实现字典(dict)的迭代操作示例:
d = {'Adam': 95, 'Lisa': 85, 'Bart': 59, 'Paul': 74} # 迭代dict的键(以下2种方式都行) for x in d: print(x) for x in d.keys(): print(x) # 迭代dict的值 for x in d.values(): print(x) # 迭代键值对 for k, v in d.items(): print(k, ":", v)
参考:
Python 实现字典(dict)的迭代操作示例:
d = {'Adam': 95, 'Lisa': 85, 'Bart': 59, 'Paul': 74} # 迭代dict的键(以下2种方式都行) for x in d: print(x) for x in d.keys(): print(x) # 迭代dict的值 for x in d.values(): print(x) # 迭代键值对 for k, v in d.items(): print(k, ":", v)
参考: