如何从Python中减去字符串中的数字?

时间:2017-05-18 02:58:59

标签: python-2.7

我有一个数字列表:

输入P[i] = int(mycsv[i][5]) # Obtaining my string "P" including 10 int values

输出{1: 3, 2: 2, 3: 2, 4: 5, 5: 4, 6: 5, 7: 4, 8: 3, 9: 2, 10: 5}

输入d = int(mycsv[1][10]) # Obtaining my number "1", even though it is trivial

输出1

我只需要创建另一个名为" c_i"的字符串。应该这么简单:

for i in range(1,11):
    c_i[i] = P[i] - d

但是,这不起作用。显然,Python比Matlab更奇怪。任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:1)

您必须先创建字符串。尝试

c_i = ""
for i in range(1,11):
    c_i += P[i] - d

希望这有帮助。