<p>通过动态确定<code>anno</code>中每个元素的值,可能会更好:</p>
<pre><code>def anno():
for idx in xrange(248956422):
elm = "0"
for j in range(0, len(whole_st_gen)):
if whole_st_gen[j] <= idx < whole_end_gen[j]:
elm = "1"
for j in range(0, len(whole_st_ex)):
if whole_st_ex[j] <= idx < whole_end_ex[j]:
elm = "2"
for j in range(0, len(whole_st_mr)):
if whole_st_mr[j] <= idx < whole_end_mr[j]:
elm = "3"
for j in range(0, len(whole_st_nc)):
if whole_st_nc[j] <= idx < whole_end_nc[j]:
elm = "4"
yield elm
</code></pre>
<p>然后使用<code>for elm in anno()</code>进行迭代。在</p>
<p>我收到了一份编辑建议,建议对<code>whole_*_gen</code>,<code>whole_st_ex</code>等中的每个函数使用一个函数,如下所示:</p>
<p>def anno\u st():
对于X范围内的idx(248956422):
elm=“0”</p>
^{pr2}$
<p>当然,这是可行的,但它只会导致应用<code>whole_*_ex</code>的更改,并且在写入文件时需要合并这些更改,这可能有点尴尬:</p>
<pre><code>for a, b, c, d in zip(anno_st(), anno_ex(), anno_mr(), anno_nc()):
if d != "0":
write_to_file(d)
elif c != "0":
write_to_file(c)
elif b != "0":
write_to_file(b)
else:
write_to_file(a)
</code></pre>
<p>但是,如果只想应用某些更改集,可以编写一个函数,将它们作为参数:</p>
<pre><code>def anno(*args):
for idx in xrange(248956422):
elm = "0"
for st, end, tag in args:
for j in range(0, len(st)):
if st <= idx < end[j]:
elm = tag
yield tag
</code></pre>
<p>然后通过提供列表来调用(例如,只提供前两个更改):</p>
<pre><code> for tag in anno((whole_st_gen, whole_end_gen, "1"),
(whole_st_ex, whole_end_ex, "2")):
write_to_file(tag)
</code></pre>