2017-06-13 44 views
1

我實際上使用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) 
+0

游泳的平均值怎麼可能'4'?這是一個錯字嗎? – MSeifert

回答

2

可以使用字典作爲臨時變量:

values = [['Swimming', 4], ['Football', 4], ['Football', 2], ['Swimming', 2]] 

result = {} 
for activity, quotation in values: 
    if activity in result: 
     result[activity].append(quotation) 
    else: 
     result[activity] = [quotation] 

final_result = [[name, sum(quotations)/len(quotations)] for name, quotations in result.items()] 

print(final_result) 

返回:

[['Swimming', 3.0], ['Football', 3.0]] 
+0

好吧,其實@MSeifert的答案可能比我好 – Antwane

2

你可以使用一個defaultdict收集與一個「種」關聯的值,然後在列表中理解計算平均值。然而在最後的列表中元素的順序是不確定的,因爲defaultdict是無序:

from collections import defaultdict 

inp = [['Swimming', 4], ['Football', 4], ['Football', 2], ['Swimming', 2]] 

dct = defaultdict(list) 
for item in inp: 
    name, value = item 
    dct[name].append(value) 

print([[name, sum(values)/len(values)] for name, values in dct.items()]) 
# [['Swimming', 3.0], ['Football', 3.0]] 
1

您可以使用defaultdict:

a = [['Swimming', 4], ['Football', 4], ['Football', 2], ['Swimming', 2]] 
from collections import defaultdict 
d = defaultdict(lambda :0) 
d_count = defaultdict(lambda :0) 
for i in a: 
    d[i[0]] +=i[1] 
    d_count[i[0]] +=1 

d_resultat = {} 
for k,v in d.items(): 
    d_resultat[k] = v/d_count[k] 

d_resultat 

輸出:

{'Football': 3.0, 'Swimming': 3.0} 
0
import numpy as np 

#initialize list 
your_list = [['Swimming', 4], ['Football', 4], ['Football', 2]], [['Swimming', 2]] 

#initialize dict 
temp_dict = {} 
for items in your_list: 
    for item in items: 
     temp_dict.update({item[0]:[]}) 

#put values into dict 
for items in your_list: 
    for item in items: 
     temp_list = temp_dict[item[0]] 
     temp_list.append(item[1]) 
     temp_dict[item[0]] = temp_list 

#generate new list 
new_list = [] 
for key in temp_dict: 
    new_list.append((key, int(np.average(temp_dict[key]))))