试图理解浓缩的Python

时间:2017-01-27 23:19:47

标签: python

我试图了解此代码如何以长格式列出

proposed = dict((k, v) for k, v in args.iteritems() if v is not None)

我能想出的最好的是以下内容,但它不起作用:

for k,v in args.iteritems():
    print "value of v is: %s" % v
    if v is not None:
    proposed = dict(k,v)

但是它会引发以下错误:

TypeError:dict最多需要1个参数,得到2

1 个答案:

答案 0 :(得分:2)

尝试:

proposed=dict()
for k,v in args.iteritems():
  print "value of v is: %s" % v
  if v is not None:
    proposed[k] = v #This is the part you got wrong