通过字典迭代,在列表中列出

时间:2016-01-18 03:49:38

标签: python python-2.7 dictionary

我有一个字典,其中包含列表中的列表。例如

print k, value1, value2
# Output should be  first, soccer, 2

在我将数字添加到此词典之前,我会像这样迭代它们

application_controller

但现在我需要字典里面的数字,所以我试图弄清楚我如何能够按原样迭代当前的字典并且能够拉出密钥和两个值, 例如,如果我尝试这样做

module CurrentUser
  def self.included(base)
    base.send :helper_method, :current_user
  end
end

1 个答案:

答案 0 :(得分:3)

如果格式一致,您可以在迭代字典时解压缩它:

In [5]: for k, ([sport], num) in d1.iteritems():
   ...:     print k, sport, num
   ...:
second football 10
fourth rugby 3
third basketball 5
first soccer 2