Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting integer of tuple to string of tuple

I wanted to convert the integer of tuple into string of tuple. For example:

data = [(2,3,4,...),(23,42,54,...),......]

would result in:

d = [('2','3','4',...),('23','42','54',....)......]

Please notice how tuple is big and list is also big.

I tried the code below but it doesn't give me the result I was looking for:

data = [(2,3,4,...),(23,42,54,...),......] 

d=[]
for t in data:
    d.append(str(t))
like image 390
Sam Avatar asked Jan 07 '23 12:01

Sam


1 Answers

This gets close:

data = [[str(x) for x in tup] for tup in data]

If you actually need tuples:

data = [tuple(str(x) for x in tup) for tup in data]

Or, if you prefer a more "functional" approach:

data = [tuple(map(str, tup)) for tup in data]

Though it seems that map has fallen out of favor with the python crowd (or maybe it never was actually in favor ;-).

like image 155
mgilson Avatar answered Jan 17 '23 19:01

mgilson