这是我的代码:
a = {0:'000000',1:'11111',3:'333333',4:'444444'}
b = {i:j+'www' for i,j in a.items()}
print b
它显示错误:
File "g.py", line 7
b = {i:j+'www' for i,j in a.items()}
^
SyntaxError: invalid syntax
我该如何纠正这个问题?
{i:j+'www' for i,j in a.items()}
字典理解 http://diveintopython3.org/comprehensions.html#dictionarycomprehension在 Python 3 中工作得很好。
正如你在这里看到的:http://ideone.com/tbXLA http://ideone.com/tbXLA(注意,我在 Python 3 中将 print 称为函数)。
如果你有
要实现这种类型的概念,您必须执行列表/生成器表达式来创建键、值的元组。一旦发生这种情况,您可以调用 dict() 来接受元组列表。
dict((i,j+'www') for i,j in a.items())
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)