2016-11-21 36 views
0

好的幫助,所以我需要,將採取一個CSV文件,並在讀取值(到目前爲止我已經得到了一部分下來)代碼。需要使用詞典,CSV文件,並列出

什麼我在與正在創建這些值的列表,並在少順序排序他們帶來麻煩再次發生的最重發生。不能有重複的值。

這是我有:

import csv 
B = [] 
K = [] 
def readandprocess(name): 
with open(name, newline='') as csvf: 
    freader = csv.reader(csvf,delimiter=',',quotechar='"') 
    datasg = {} 
    artists = [] 



    for row in freader: 
     artist = row[2] 
     B.append(artist) 


    for artist in B: 
      c = B.count(artist) 
      K.append(artist + str(c)) 


    list(set(K)) 
    print(K) 

    #for row in freader: 
     #artist = row[2] 
     ###song = row[1] 
     #if artist == 'Rolling Stones': 
     # print('Rolling Stones title: ',row[1]) 
     #if artist not in datasg: 
     # datasg[artist] = [song] 
     #else: 
      #datasg[artist].append(song) 
    #for artist in datasg: 
     #print(artist, datasg[artist]) 
    print('--------------------------------------') 
    info = datasg.items() 
    # tosort = [(len(t[1]),t[0]) for t in info] 
    # info = sorted(tosort) 
    # print(info[-30:]) 
    # print(info) 

print(len(datasg)) # currently 0, populate at will #Number of keys in dictionary 
return datasg 


if __name__ == '__main__': 
    datasg = readandprocess('data/top1000.csv') 

回答

0

使用Counter嘗試。一旦列出了所有需要的項目,您可以使用計數器,然後致電most_common(n)以獲取最常見的元素。

+0

似乎可用,但我的蟒蛇不違背承認一個函數,什麼纔是我需要進口來解決這一問題? –

+0

'從集合進口計數器' – Alex

+0

NVM我得到它的工作!你是救命的亞歷克斯! –