我實際上使用Google Chart API和Flask。我從我的數據庫中檢索數據,然後將它們添加到我的列表中。意味着在列表中有兩個元素
我的列表:
[['Swimming', 4], ['Football', 4], ['Football', 2]], [['Swimming', 2]
我想例如用於'Football'
平均等有['Football', 3]
。 'Swimming'
相同。所以預期的輸出將是:
[['Swimming', 4], ['Football', 3]]
我該怎麼做?
代碼:
def get_quotation_by_activity(self, id_child):
cursor = self.link.cursor()
try:
cursor.execute("SELECT previous_activity, quotation FROM sessions, sessions_questions_asked WHERE sessions.id = sessions_questions_asked.id_session AND child=%s;", [id_child]);
quotation_activity = []
quotation_activity_mean = []
for row in cursor:
quotation_activity.append([row[0], row[1]])
except (Exception, psycopg2.DatabaseError) as error:
print(error)
return []
print(quotation_activity)
#Some tests..
'''for i in quotation_activity:
activity = i[0]
quotation = i[1]
if not i in quotation_activity_mean:
quotation_activity_mean.append(i)'''
print(quotation_activity_mean)
return quotation_activity
@app.route("/statistics")
def stats():
quotation_activity = db.get_quotation_by_activity(6)
return render_template('statistics.html', quotation_activity= quotation_activity)
游泳的平均值怎麼可能'4'?這是一個錯字嗎? – MSeifert