如何在Python中将列表的元素从字符串转换为整数?

时间:2016-05-04 00:17:50

标签: python integer int

我必须对矩阵进行乘法运算,但我必须逐行给出这些元素。我知道我必须将它们写成字符串,但我不知道将它们转换为int / integer。

for i in range (0,orden):
    b= input("Write the numbers of the row, of the first matrix")
    a.append(b)
    ar.append(a)
    a=[]

2 个答案:

答案 0 :(得分:0)

请尝试使用int(s)将字符串转换为整数。

答案 1 :(得分:-1)

如果在名为row的字符串中有一行整数,则应首先执行:

row = "10 2 3775"
row_as_strings = row.split(" ")

然后,要将字符串列表转换为整数,请执行:

row_as_ints = list(map(int, row_as_strings))

如果您只需要将字符串(称为my_string)转换为int,请执行:

int(mystring)