我有一堆被排序的互作式(其条目代表基因组上的位置)。我想同时处理它们,以便处理所有在特定位置具有数据的数据,而忽略不包含数据的数据。
一个比较笨拙的解决方案是
import sys
it1 = iter([0, 1, 3, 5])
it2 = iter([0, 2, 4, 5])
it3 = iter([3, 5, 7])
def do_stuff(objects, values):
print("iterables [%s] have value %s" % (objects, values[0]))
def iterate_many(iterables):
cur_ele = [next(i) for i in iterables]
is_finished = [False for i in iterables]
while not all(is_finished):
lowest_val = min(cur_ele)
lowest_obj = [e for e in cur_ele if e == lowest_val]
to_increment = [i for i, e in enumerate(cur_ele) if e == lowest_val
and not is_finished[i]]
yield do_stuff(to_increment, lowest_obj)
for i in to_increment:
try:
cur_ele[i] = next(iterables[i])
except StopIteration:
is_finished[i] = True
cur_ele[i] = sys.maxsize
结果
In [76]: for i in iterate_many( [it1, it2, it3]): pass
iterables [[0, 1]] have value 0
iterables [[0]] have value 1
iterables [[1]] have value 2
iterables [[0, 2]] have value 3
iterables [[1]] have value 4
iterables [[0, 1, 2]] have value 5
iterables [[2]] have value 7
有没有更简单/更Python化的方式来实现这一目标?
答案 0 :(得分:3)
from itertools import zip_longest
for a, b, c in zip_longest(it1, it2, it3)
print a, b, c
https://docs.python.org/3/library/itertools.html#itertools.zip_longest