for (@foo) { # do something next if $seen{$_}++; }
我希望能够在Python中执行等效操作,即如果已执行一次则跳过块.
seen = set() for x in foo: if x in seen: continue seen.add(x) # do something
有关更多信息,请参阅set文档.
set
此外,itertools模块文档底部的示例包含unique_everseen生成器,您可以像这样使用:
for x in unique_everseen(foo): # do something