【发布时间】:2017-10-28 03:46:32
【问题描述】:
有人可以帮助我了解以下语法或告诉我是否可能吗?因为我要修改if ... else ... 条件。我不想在列表中添加重复值,但我得到了KeyError。
其实这种说法我并不熟悉:
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]
【问题讨论】:
标签: python python-3.x if-statement