1

我遇到了一些 Python v2.7 代码,我将这些代码重构为:

for ssid in (ssid for ssid in overlaps
             if ssid != subseq_id and ssid not in merged):

但是我觉得应该有一种方法来表达这一点而无需使用两个for语句?

4

2 回答 2

14

你可以使用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
    # ...
于 2013-02-28T21:51:16.387 回答
0

根据您的数据(顺序并不重要,...),您可以使用 sets :

excluded = set(merged)
excluded.add(subseq_id)
ssids = set(overlaps) - excluded
for ssid in ssids:
    ...
于 2013-02-28T22:12:34.797 回答