我在python中有一个关于sqlite3的问题,它是关于列表问题的。 这是一个问题:
编写一个函数getMay(dbName)
,该函数将上述数据库的文件名作为参数,并返回两个列表,一个列出日期,另一个列出当天中午的温度。
这是我的代码:
import sqlite3
def getMay(dbName):
conn = sqlite3.connect(dbName)
cur = conn.cursor()
cur.execute('select Day,Temp from May14 where Time= "12:00" order by Day ASC')
print(cur.fetchall())
cur.close()
conn.close()
这是我的输出:
[(1,13.7),(2,11.1),(3,12.2),(4,13.2),(5,12.9),(6,12.5),(7, 9.6),(8,11.6),(9,13.2),(10,19.2),(11,21.7),(12,15.2),(13,11.9),(14,16.4),(15,12.2) ,(16,10.1),(17,9.8),(18,16.2),(19,21.5),(20,17.8),(21,17.0),(22,18.6),(23,16.5),( 24,21.2),(25,25.4),(26,27.8),(27,27.3),(28,13.7),(29,15.0),(30, 22.5),(31,21.0)]
但是正确的输出应该如下:
([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31], [13.7, 11.1, 12.2, 13.2,
12.9, 12.5, 9.6, 11.6, 13.2, 19.2, 21.7, 15.2, 11.9, 16.4, 12.2, 10.1,
9.8, 16.2, 21.5, 17.8, 17.0, 18.6, 16.5, 21.2, 25.4, 27.8, 27.3, 13.7,
15.0, 22.5, 21.0])
任何人都知道如何解决这个问题? 请帮忙!谢谢!
答案 0 :(得分:0)
一个简单的解决方案可能是:
dayTemp_list = [(1, 13.7), (2, 11.1), (3, 12.2), (4, 13.2), (5, 12.9), (6, 12.5), (7, 9.6), (8, 11.6), (9, 13.2), (10, 19.2), (11, 21.7), (12, 15.2), (13, 11.9), (14, 16.4), (15, 12.2), (16, 10.1), (17, 9.8), (18, 16.2), (19, 21.5), (20, 17.8), (21, 17.0), (22, 18.6), (23, 16.5), (24, 21.2), (25, 25.4), (26, 27.8), (27, 27.3), (28, 13.7), (29, 15.0), (30, 22.5), (31, 21.0)]
days = []
temp = []
for i in dayTemp_list:
days.append(i[0])
temp.append(i[1])
result = (days,temp)
print result