将列表中的字符串转换为整数

时间:2015-03-22 22:35:58

标签: python string list int type-conversion

v=["there", 3, "9","hello",25]
x=values[1]+values.int[2]
#I want to make 9 an integer so I could get the sum of 3 and 9. Im getting an 
error

我已经尝试在值[2]上使用int函数,我收到了错误 Traceback(最近一次调用最后一次):   文件“/Users/tigersoprano/Documents/text2.py”,第9行,in     G =值[1] + values.int [2] AttributeError:'list'对象没有属性'int'

2 个答案:

答案 0 :(得分:0)

首先,您的数组名为v,而不是values 其次,int函数是int()

所以:

v=["there", 3, "9","hello",25]
x=v[1]+int(v[2])

应该工作

答案 1 :(得分:0)

>>> v=["there", 3, "9","hello",25]
>>> x=v[1]+int(v[2])
12

你得到一个'属性错误'因为'列表' type没有名为' int'的属性,即' v.int'不会做任何事情。要将字符串转换为整数,可以执行以下操作

>>>int("9")
9