0
例如:有沒有一種方法可以縮短Python循環遍歷字典的循環?
dic = {'a': 'b', 'c': 'd'}
for x, y in dic:
print(x,y)
在理想情況下返回
a b
但它拋出一個錯誤信息。那麼,有沒有替代這樣做:
for x in dic:
y = dic[x]
例如:有沒有一種方法可以縮短Python循環遍歷字典的循環?
dic = {'a': 'b', 'c': 'd'}
for x, y in dic:
print(x,y)
在理想情況下返回
a b
但它拋出一個錯誤信息。那麼,有沒有替代這樣做:
for x in dic:
y = dic[x]
試試這個:
for x, y in dic.items():
print(x, y)