简体   繁体   English

打印python列表中项目的组合长度

[英]Printing the combined length of items in list for python

I'm trying to find out the combined length of all of the items inside of a list. 我试图找出列表中所有项目的总长度。

list = [12, 35]

i'm not quite sure how to calculate the len of all the items inside in one go, so that it returns '4', instead 2, the amount of items in the list. 我不太确定如何一次性计算所有内部项目的len,以便它返回“ 4”,而不是列表中项目的2。 Thanks! 谢谢!

I assume that the length of a number is given by the number of digits. 我假设一个数字的长度是由位数给出的。

The easiest way would be to convert everything to a string and then add their lengths: 最简单的方法是将所有内容都转换为字符串,然后添加其长度:

>>> lst = [12, 35]
>>> sum(len(str(item)) for item in lst)
4

稍微更pythonic。

len(''.join(map(str, lst)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM