我遇到了一些 Python v2.7 代码,我将这些代码重构为:
for ssid in (ssid for ssid in overlaps
if ssid != subseq_id and ssid not in merged):
但是我觉得应该有一种方法来表达这一点而无需使用两个for
语句?
我遇到了一些 Python v2.7 代码,我将这些代码重构为:
for ssid in (ssid for ssid in overlaps
if ssid != subseq_id and ssid not in merged):
但是我觉得应该有一种方法来表达这一点而无需使用两个for
语句?
你可以使用filter()
:
for ssid in filter(lambda s: s != subseq_id and s not in merged, overlaps):
# ...
或者你可以在循环中测试:
for ssid in overlaps:
if ssid == subseq_id or ssid in merged:
continue
# ...
根据您的数据(顺序并不重要,...),您可以使用 sets :
excluded = set(merged)
excluded.add(subseq_id)
ssids = set(overlaps) - excluded
for ssid in ssids:
...