【问题标题】:A problem with convertion from string to float in Python 3.8Python 3.8 中从字符串转换为浮点的问题
【发布时间】:2019-11-02 19:48:19
【问题描述】:

我创建了等于 ['jan', 'feb', 'mar', '451'] 的 y 列表 我试图将 y[3] 的类型转换为浮点数,但是当我打印 y[3] 的类型时,它似乎是一个字符串。

有什么问题?

 >>>x = "jan feb mar 451"
 ...y = x.split()
 ...float(y[3])
 ...type(y[3])
 str

【问题讨论】:

    标签: python-3.x string type-conversion


    【解决方案1】:

    您需要分配浮点转换的结果,例如z = float(y[3]) 或者如果你想用浮点数替换你的字符串值,你可以使用y[3]=float(y[3])

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-25
      • 2020-10-12
      • 2018-12-08
      • 2016-06-08
      • 1970-01-01
      • 2019-07-10
      • 1970-01-01
      相关资源
      最近更新 更多