Summing elements in a list

前端 未结 7 777
我在风中等你
我在风中等你 2020-11-30 23:16

Here is my code, I need to sum an undefined number of elements in the list. How to do this?

l = raw_input()
l = l.split(\' \')
l.pop(0)

My

7条回答
  •  -上瘾入骨i
    2020-12-01 00:11

    >>> l = raw_input()
    1 2 3 4 5 6 7 8 9 10
    >>> l = l.split()
    >>> l.pop(0)
    '1'
    >>> sum(map(int, l)) #or simply sum(int(x) for x in l) , you've to convert the elements to integer first, before applying sum()
    54
    

提交回复
热议问题