How to get another variable type from sql query
From this query...
self.cur.execute("""SELECT pub FROM "20091229"
GROUP BY pub ORDER BY pub""")
I get the following list with tuples back.
>&开发者_运维技巧gt;[(1,), (2,), (3,)]
But I need only a list or a list without decimal points in the tuples. Something like these:
[1, 2, 3]
or
[(1), (2), (3)]
Thanks.
It will be better if you tell us what module do you use to work with db. Also i am not sure but your query can be optimized to something like this 'SELECT DISTINCT pub FROM "20091229" ORDER BY pub'
I know that nobody here likes reduce but it could be also used here:
from operator import add
reduce(lambda res,x: add(res, x), [(1,), (2,), (3,)])
OR in case there could be only list of single item tuples:
from operator import itemgetter
map(itemgetter(0), [(1,), (2,), (3,)])
What about this?
li = [tup[0] for tup in li]
Please note, a tuple get's written like this (1,)
, because (1)
would imply mathematical brackets. Also, it seems you are trying to print a list, consider using a dedicated printing function.
精彩评论