Convert a string of numbers to a list of integers. Python

后端 未结 1 497
挽巷
挽巷 2021-01-26 12:39

I am trying to convert the string str1 to a list of numbers so that I could sum them up. First I use the split() function to make sense of the numbers in str1, I cast the string

相关标签:
1条回答
  • 2021-01-26 13:06

    Using split() will not split up str1, as without the sep argument the default separator is a space ' '. Hence:

    str2 == ["13,22,32,4,5"]
    

    you need to specify that split should use a comma ','. In fact, you can combine your operations into one:

    sum(map(int, str1.split(',')))
    
    0 讨论(0)
提交回复
热议问题