我正在學習字典,並且編寫了一小段代碼並構建它。Python OrderedDict通過int迭代
from collections import OrderedDict
d1 = OrderedDict([('a',0),('b',9),('c',8),('d',7),('e',6)])
d2 = OrderedDict([(1),(2),(3),(4),(5)])
我得到以下錯誤:
d2 = OrderedDict([(1),(2),(3),(4),(5)])
TypeError: 'int' object is not iterable
我不明白爲什麼它沒有可迭代? 'int'
與這個問題有什麼關係?
由於
'(1)'是整數1. OrderedDict期望兩元素迭代(帶有一個鍵和一個值)。 – RemcoGerlich