【问题标题】:How can i convert to integer numbers from string numbers in python如何从python中的字符串数字转换为整数
【发布时间】:2020-05-17 18:09:04
【问题描述】:

我想更改类型我的字符串数字。 我想让它们成为整数,以便比较它们哪个更大。

numbers="4, 5, 29, 54, 4 ,0 ,-214, 542, -64, 1 ,-3, 6, -6"

ns=numbers.split()
nsi=[]
for i in range(len(ns)):
    ns[i]=int(ns[i])
    nsi.append(ns[i])


print(nsi)

我遇到这样的错误

ns[i]=int(ns[i])
ValueError: invalid literal for int() with base 10: '4,'

【问题讨论】:

  • 只要ns = [int(n) for n in numbers.split(',')] 工作正常。
  • list(map(int, numbers.split(","))) 也可以。

标签: python string if-statement type-conversion integer


【解决方案1】:

问题是split() 使用空格作为默认拆分字符。所以还剩下逗号。您可以使用split(', ') 指定函数的分隔符。

请参阅this 示例了解如何使用拆分。

【讨论】:

    【解决方案2】:

    你犯了一个错误。

    改变这一行

    ns=numbers.split()
    

    进入这个

    ns=numbers.split(", ")
    

    【讨论】:

      【解决方案3】:

      你可以的

      numbers="4, 5, 29, 54, 4 ,0 ,-214, 542, -64, 1 ,-3, 6, -6"
      ns = [int(i) for i in numbers.split(",")]
      nsi=[]
      for i in range(len(ns)):
         nsi.append(ns[i])
      print(nsi)
      

      numbers="4, 5, 29, 54, 4 ,0 ,-214, 542, -64, 1 ,-3, 6, -6"
      ns = [int(i) for i in numbers.split(",")]
      nsi=[]
      nsi.extend(ns)
      print(nsi)
      

      这会将数字列表中的所有数字转换为整数。 输出

      [4, 5, 29, 54, 4, 0, -214, 542, -64, 1, -3, 6, -6]
      

      【讨论】:

      • 为什么是第二个循环?为什么不直接指定nsi 开始呢?
      • 只是因为它在请求中
      • 非常感谢您的回复。我很感激你:)
      • 欢迎您,欢迎来到 SO。如果答案对您有帮助,请投票并将其标记为已回答。
      【解决方案4】:

      当你分割字符串时,它被空格分割......所以每个数字仍然有逗号。

      固定:

      numbers="4, 5, 29, 54, 4 ,0 ,-214, 542, -64, 1 ,-3, 6, -6"
      
      ns=numbers.replace(",", "").split()
      nsi=[]
      for i in range(len(ns)):
          ns[i]=int(ns[i])
          nsi.append(ns[i])
      
      print(nsi)
      ns[i]=int(ns[i])
      

      【讨论】:

        【解决方案5】:

        当您尝试将数组元素转换为整数时,使用.split(", ") 会报错,因为 numbers.split(", ") 将输出为

        ns = numbers.split(", ")
        >>> print ns
        ['4', '5', '29', '54', '4 ,0 ,-214', '542', '-64', '1 ,-3', '6', '-6'] //'4 ->incorrect output
        >>> print int(ns[4])
        Traceback (most recent call last):
          File "<stdin>", line 1, in <module>
        ValueError: invalid literal for int() with base 10: '4 ,0 ,-214'
        >>>
        

        相反,你只能这样做

        numbers.split(",")
        
        

        它会给你正确的整数数组。 然后为您的目的将元素类型转换为 int(...)

        >>> ns = numbers.split(",")
        >>> print ns
        ['4', ' 5', ' 29', ' 54', ' 4 ', '0 ', '-214', ' 542', ' -64', ' 1 ', '-3', ' 6', ' -6']
        >>> a = int(ns[1])
        >>> print a
        5
        

        【讨论】:

          【解决方案6】:

          你可以在 split() 中使用不同的分隔符。

          numbers="4, 5, 29, 54, 4 ,0 ,-214, 542, -64, 1 ,-3, 6, -6"
          
          ns=numbers.split(sep=",")
          nsi=[]
          for i in range(len(ns)):
             ns[i]=int(ns[i])
             nsi.append(ns[i])
          
          print(nsi)
          

          【讨论】:

            猜你喜欢
            • 2014-09-19
            • 1970-01-01
            • 2019-11-26
            • 2010-12-27
            • 2013-02-18
            • 1970-01-01
            • 2015-07-04
            • 1970-01-01
            相关资源
            最近更新 更多