例如,有没有办法轻松地将列表中的数字转换为个位数
population = ['10001','11111','11010','110001']
into ['1','0','0','0','1']
add each digit in each set and put it in another list like
this
evaluation = [2,5,3,3] (adding up all the 1's up on the first list)
我对 Python 很陌生,所以我不确定我是否正确地做这件事
一种可能的方法是遍历population
列表并使用str.count('character')计算'1'
每个“数字”字符串中的 ':
evaluation = list(item.count('1') for item in population)
evaluation
将包含所需的计数:
>>> print(evaluation)
[2, 5, 3, 3]
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句