我想在数组中的tuble中添加反斜杠所有双点...
我的代码:
def removeDots (input):
for i in input:
p = i[2]
p.replace(":","\:")
liste = list(i)
liste.append(p)
liste = tuple(i)
return input
示例:
Before: [('Hello:bye','Hello:bye'),('Bye:Hello','Bye:Hello')]
After: [('Hello:bye','Hello\:bye'),('Bye:Hello','Bye\:Hello')]
答案 0 :(得分:1)
这应该有效:
def remove_dots(data):
return [tuple(s.replace(":", "\:") for s in row) for row in data]
答案 1 :(得分:0)
以下是我认为您正在寻找的基本示例。您需要根据具体情况进行调整。
def removeDots(x):
p = []
for i in x:
p.append((i[0],i[1].replace(":","\\:")))
return p
x = [('Hello:bye','Hello:bye'),('Bye:Hello','Bye:Hello')]
print removeDots(x)
请记住逃避反斜杠!
答案 2 :(得分:0)
def removeDots (x):
p = []
for i in x:
p.append((i[0],i[1].replace(":","\\:")))
return p
x = [('Hello:bye','Hello:bye'),
('Bye:Hello','Bye:Hello')]
print x
print removeDots(x)
答案 3 :(得分:0)
def removeDots(input):
res = list()
for x, y in input:
res.append((x,y.replace(":","\\:")))
return res