如何更改特定的字符串索引

时间:2019-06-13 09:33:01

标签: python-3.6

a=(["id': 'tl_00'}"], ["index': '9',"], ["resp': '1110000000001111',"], ["fors': '1110000000001111'}"])

我想创建一个新列表,以便删除所有}

a=a=(["id': 'tl_00'}"], ["index': '9',"], ["resp': '1110000000001111',"], ["fors': '1110000000001111'}"])
b=""
for i in range(len(a)):
    for j in range(len(a[i])):
        for k in range(len(a[i][j])):
            b+=a[i][j][k]
            if a[i][j][k]=="}":
                b[i][j][k]+=""

错误是列表索引超出范围。有更好的方法吗?

2 个答案:

答案 0 :(得分:1)

使用str.strip()

例如:

a=(["id': 'tl_00'}"], ["index': '9',"], ["resp': '1110000000001111',"], ["fors': '1110000000001111'}"])
b=[[k.strip("}") for k in sublist] for sublist in a]
print(b)

输出:

[["id': 'tl_00'"],
 ["index': '9',"],
 ["resp': '1110000000001111',"],
 ["fors': '1110000000001111'"]]

答案 1 :(得分:0)

我想添加一个更接近您原始代码的替代方法:

class Person {
    static let details = Person()
    var name = "Alan Turing"
    let age = "42"
}