Python:计算列表中的数字:

2024-06-26 02:15:32 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我代码的一部分https://stackoverflow.com/questions/21617079/python-help-in-organizing-the-output-in-colums的链接

现在我需要做的是假设我的输出

主持人 100010个

101010年

101001个

1001个

寄生虫 10001001 10001000 10010001 没有 没有 没有

对于程序的下一部分,我需要计算寄生虫列表中的数字序列。例如,对于寄生虫部分,这应该是3。(我基本上什么都拿-(没有的数字)我该怎么做。你知道吗


Tags: the代码inhttpscomoutput链接help
2条回答

你的问题有点言过其实。但是。看起来列表中的元素要么是子列表(包含某些内容,无关紧要),要么是None。所以:

parasite = [[1,0,1,1,0,0],[0,1,0,0,0,0],[0,0,0,0,0,0],None,None,None]

sum(map(bool,parasite))
Out[27]: 3

"I basically take everything- (the number of none)"

在Python中:

>>> parasite = [[1,0,1,1,0,0],[0,1,0,0,0,0],[0,0,0,0,0,0],None,None,None]
>>> len(parasite) - parasite.count(None)
3

相关问题 更多 >