我需要从字符串类型转换为数字
list = ["-5","4","-3","variable"] # Input
list = [-5,4,-3,"variable"] # epected output
我在转换负数时遇到问题
list[0]=int(list[0])
ValueError:以 10 为底的 int() 的无效文字:’-5’
原文由 Edinson Enrique Sanchez Galan 发布,翻译遵循 CC BY-SA 4.0 许可协议
实际上,我无法重现你的错误,它只适用于 python 2.7.13
在 python 3.4 中
所以,这可能是 python 版本问题,我建议更新你的 python 版本,通过从 站点 重新安装更新的版本,它将完美地替换它(包不变),除非你正在使用像 anaconda 这样的特殊发行版。
要处理您的列表(混合字符和数字),请使用:
try: except:
声明。