【发布时间】:2017-02-04 01:53:28
【问题描述】:
以下代码输出:
import itertools as it
list(it.product(['A', 'T'], ['T', 'G']))
Out[230]: [('A', 'T'), ('A', 'G'), ('T', 'T'), ('T', 'G')]
但是,如果列表是:
['A', '/', 'T'], ['T', '/', 'G']
['C', '|', 'T'], ['A', '|', 'C']
我想要
[('A', 'T'), ('A', 'G'), ('T', 'T'), ('T', 'G')]
[('C', 'A'), ('T', 'C')]
意思是:
list(it.product(['A', '/', 'T'], ['T', '/', 'G'])) if '/' in the list
else list(it.product(['A', '/', 'T'], ['T', '/', 'G'])) if '|' in the list
如何在不删除 / 和 | 的情况下获得相同的结果,因为这是一个条件。
我认为使用索引可能会起作用并尝试过类似的方法:
list(it.product(['A', '/', 'T'], ['T', '/', 'G']).index([0,2))
和其他几个过程,但没有帮助。
这段代码是大代码的尾部,所以我不想构建任何函数或删除/。
【问题讨论】:
标签: python list product indexof itertools