我会这样:
seen = set()cond = [x for x in c if x[3] not in seen and not seen.add(x[3])]
说明:
seen是一个跟踪每个子列表中第四个元素的集合。
cond是精简清单。如果
x[3](其中
x的子列表中的
c)不在其中
seen,
x则将添加到中
cond并将
x[3]添加到中
seen。
seen.add(x[3])将返回
None,所以
not seen.add(x[3])总是会
True,但如果这部分将只进行评估
x[3]not inseen是
True因为Python使用短路评价。如果第二个条件得到评估,它将始终返回
True并具有添加
x[3]到的副作用
seen。这是正在发生的事情的另一个示例(
None并具有打印某些内容的“副作用”):
>>> False and not print('hi')False>>> True and not print('hi')hiTrue


