我的输出看起来像这样
100010
101010
101001
None
None
None对于程序的下一部分,我需要计算数字序列的数量。举个例子,应该是3,我该怎么做呢?程序的第一部分基本上生成了一个随机的二进制序列(代表寄生虫基因组),而none意味着宿主没有寄生虫。这些是随机生成的。(每次运行程序时,这可能会有所不同)。我只需要计算那里有多少寄生虫。这应该在python中完成。
发布于 2014-02-10 13:03:47
使用generator expression和sum
>>> lst = [1000010, 101010, 101001, None, None, None]
>>> sum(x != None for x in lst)
3另一种方法是:从总数中减去None的数量:
>>> len(lst) - lst.count(None)
3发布于 2014-02-10 13:05:02
input = '100010 101010 101001 None None None'
import string
wordsList = string.split(input)
i=0;
for word in wordsList:
if word != 'None' :
i= i+1
print i'i‘会给出结果3
发布于 2014-02-10 12:59:06
In [2]: L = [100010, 101010, 101001, None, None, None]
In [3]: sum(1 for _ in itertools.takewhile(lambda n: n is not None, L))
Out[3]: 3https://stackoverflow.com/questions/21669276
复制相似问题