我有这个列表:
>>> stud
[19, 11, 6, 26]
如果我遍历'draws'
字段,则会得到以下信息:
>>> [aluDict[str(s)]['draws'] for s in stud]
[1, 0, 1, 0]
现在,我想向其中的每一个添加1
,以便(可能)获得以下内容:
>>> [aluDict[str(s)]['draws'] for s in stud]
[2, 1, 2, 1]
为此,我尝试了以下操作:
>>> [aluDict[str(s)]['draws']+=1 for s in stud]
File "<stdin>", line 1
[aluDict[str(s)]['draws']+=1 for s in stud]
^
SyntaxError: invalid syntax
如果我单独进行操作,则可以做到:
>>> aluDict[str(19)]['draws']
2
>>> aluDict[str(19)]['draws']+=1
>>> aluDict[str(19)]['draws']
3
为什么会这样?我想念什么?
谢谢!
答案 0 :(得分:0)
如果您只想要列表[2, 1, 2, 1]
,并且不想实际更改存储在aluDict
中的任何值,请使用+1
:
[aluDict[str(s)]['draws']+1 for s in stud]