Python: Sum string lengths

前端 未结 7 1317
暗喜
暗喜 2020-12-06 17:17

Is there a more idiomatic way to sum string lengths in Python than by using a loop?

length = 0
for string in strings:
    length += len(string)
相关标签:
7条回答
  • 2020-12-06 17:47

    Here's another way using operator. Not sure if this is easier to read than the accepted answer.

    import operator
    
    length = reduce(operator.add, map(len, strings))
    
    print length
    
    0 讨论(0)
提交回复
热议问题