1
有人可以帮我使用下面的语法,或者告诉我它是否可行吗?因为我要修改if ... else ...
条件。我不想在列表中添加重复的值,但我得到了KeyError
。Python内联if语句
其实,我不熟悉这样的语句:
twins[value] = twins[value] + [box] if value in twins else [box]
是什么恰恰意味着?
示例代码
#dictionary
twins = dict()
#iterate unitlist
for unit in unitlist:
#finding each twin in the unit
for box in unit:
value = values[box]
if len(value) == 2:
twins[value] = twins[value] + [box] if value in twins else [box]
我改性条件
#dictionary
twins = dict()
#iterate unitlist
for unit in unitlist:
#finding each twin in the unit
for box in unit:
value = values[box]
if len(value) == 2:
if value not in twins:
twins[value] = twins[value] + [box]
实际上,“双胞胎[价值] = tmp”应放在if-else内。谢谢 – KDB