Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Lits containg tuples and long int

I have a list containing a tuples and long integers the list looks like this:

   table = [(1L,), (1L,), (1L,), (2L,), (2L,), (2L,), (3L,), (3L,)]

How do i convert the table to look like a formal list?

so the output would be:

table = ['1','1','1','2','2','2','3','3']

For information purposes the data was obtained from a mysql database.

like image 431
Yasmin Avatar asked Mar 07 '26 23:03

Yasmin


2 Answers

>>> table = [(1L,), (1L,), (1L,), (2L,), (2L,), (2L,), (3L,), (3L,)]
>>> [int(e[0]) for e in table]
[1, 1, 1, 2, 2, 2, 3, 3]

>>> [str(e[0]) for e in table]
['1', '1', '1', '2', '2', '2', '3', '3']
like image 188
Emil Ivanov Avatar answered Mar 09 '26 14:03

Emil Ivanov


With itertools

import itertools

>>> x=[(1L,), (1L,), (1L,), (2L,), (2L,), (2L,), (3L,), (3L,)]
>>>
>>> list(itertools.chain(*x))
[1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L]
>>>
>>> map(str,itertools.chain(*x))
['1', '1', '1', '2', '2', '2', '3', '3']
like image 38
YOU Avatar answered Mar 09 '26 14:03

YOU