简体   繁体   中英

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. 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)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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