my_dict = {'apple':'der Apfel', 'ant':'die Ameise', 'asparagus':'der Spargel'}
print 'dictionary for a'
for k, v in my_dict.iteritems():
print '%s:%s' % (k, v)
>>> dict_ = {'apple':'der Apfel', 'ant':'die Ameise', 'asparagus':'der Spargel'}
>>> for k,v in dict_.iteritems():
... print k, ':', v
...
ant : die Ameise
asparagus : der Spargel
apple : der Apfel
这能做你想做的吗?在
(编辑:更新为新的输出格式)
产量:
^{pr2}$请注意,此订单与您发布的订单不同,但问题并未说明订单是否重要。在
正如@wim正确建议的那样,最好不要使用
dict
作为变量名。在如果您只是尝试遍历字典,以打印键和值对:
在一行中使用生成器表达式:
^{pr2}$另外,请避免使用
dict
作为变量名,因为它隐藏了内置的内容。在如果您只想将每个键和值对打印成一行,可以执行以下操作:
输出的顺序可能与创建字典时的顺序不同。迭代字典并不能保证任何特定的顺序,除非您专门对键进行排序,例如使用sorted()内置函数:
^{pr2}$相关问题 更多 >
编程相关推荐