-4

I would like to add a backslash all doublepoints in a tuble that is in a array...

The code I have:

def removeDots (input):
    for i in input:
        p = i[2]
        p.replace(":","\:")
        liste = list(i)
        liste.append(p)
        liste = tuple(i)
    return input

Example:

Before: [('Hello:bye','Hello:bye'),('Bye:Hello','Bye:Hello')]
After: [('Hello:bye','Hello\:bye'),('Bye:Hello','Bye\:Hello')]
itspat
  • 56
  • 1
  • 7

4 Answers4

1

This should work:

def remove_dots(data):
    return [tuple(s.replace(":", "\:") for s in row) for row in data]
Alex Hall
  • 34,833
  • 5
  • 57
  • 89
  • This outputs: `[('H', 'e', 'l', 'l', 'o', '\\:', 'b', 'y', 'e'), ('B', 'y', 'e', '\\:', 'H', 'e', 'l', 'l', 'o')]` for `remove_dots([('Hello:bye'),('Bye:Hello')])` – Lafexlos Apr 28 '16 at 14:22
  • @Lafexlos but that input is incorrect and has been changed in the question. Each element of the input should be a tuple, not just a string. – Alex Hall Apr 28 '16 at 14:24
  • Sorry, then. My bad. Didn't notice that. – Lafexlos Apr 28 '16 at 14:25
  • Returned erroe `TypeError: an integer is required` – itspat Apr 28 '16 at 14:32
  • @AlexHall `def remove_dots(x): return [tuple(s.replace(":", "\:") for s in row) for row in x] resultSiDynamicData = cur.fetchall() resultSiDynamicDataReturn = remove_dots(resultSiDynamicData)` – itspat Apr 28 '16 at 14:38
1
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)
Stefano
  • 3,981
  • 8
  • 36
  • 66
0

Here's a basic example of what i think you're looking for. You will need to adjust it to suit your case specifically.

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)

Remember to escape the backslash!

DCA-
  • 1,262
  • 2
  • 18
  • 33
0
def removeDots(input):
    res = list()
    for x, y in input:
        res.append((x,y.replace(":","\\:")))
    return res
Daniel
  • 573
  • 6
  • 14