一般用python查詢MySQL后,返回的結果是list或者tuple,如何取某一列數據呢? mysql的返回值可以是tuple也可以是dict,常用的時tuple。 有時候想要根據前一個SQL的結果去生成另一個SQL,比如: sql1 = select id,name from article limit 10; sql2 = select * from reply where article_id in (sql1.id); 如是就有了下面的問題: (('a',), ('b',), ('c',), ('d',), ('e',), ('f',)) 到 'a','b','c','d','e','f' 怎么做到呢? 使用這個,不再拼接字符串: >>> a=(('a',1), ('b',2), ('c',3), ('d',4), ('e',5), ('f',6)) >>> id,name = zip(*a) >>> print str(id); ('a', 'b', 'c', 'd', 'e', 'f') >>> print str(name); (1, 2, 3, 4, 5, 6)
python zip 解壓 zip(*迭代對象)
zip(*a)
