Skip to main content
 首页 » 编程设计

python之试图理解精简的 Python

2024年10月01日11jillzhang

我试图理解这段代码如何以长格式布局

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 expected at most 1 arguments, got 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