我有一個文件,其中包含樂隊列表以及專輯的製作年份。 我需要編寫一個函數來查看這個文件,並找出這些樂隊的不同名稱,並計算出這些樂隊在這個文件中出現的次數。用某些詞彙打印每個短語/單詞的頻率?
文件的樣子是這樣的:
Beatles - Revolver (1966)
Nirvana - Nevermind (1991)
Beatles - Sgt Pepper's Lonely Hearts Club Band (1967)
U2 - The Joshua Tree (1987)
Beatles - The Beatles (1968)
Beatles - Abbey Road (1969)
Guns N' Roses - Appetite For Destruction (1987)
Radiohead - Ok Computer (1997)
Led Zeppelin - Led Zeppelin 4 (1971)
U2 - Achtung Baby (1991)
Pink Floyd - Dark Side Of The Moon (1973)
Michael Jackson -Thriller (1982)
Rolling Stones - Exile On Main Street (1972)
Clash - London Calling (1979)
U2 - All That You Can't Leave Behind (2000)
Weezer - Pinkerton (1996)
Radiohead - The Bends (1995)
Smashing Pumpkins - Mellon Collie And The Infinite Sadness (1995)
.
.
.
輸出必須是在按頻率的降序,看起來像這樣:
band1: number1
band2: number2
band3: number3
這裏是我到目前爲止的代碼:
def read_albums(filename) :
file = open("albums.txt", "r")
bands = {}
for line in file :
words = line.split()
for word in words:
if word in '-' :
del(words[words.index(word):])
string1 = ""
for i in words :
list1 = []
string1 = string1 + i + " "
list1.append(string1)
for k in list1 :
if (k in bands) :
bands[k] = bands[k] +1
else :
bands[k] = 1
for word in bands :
frequency = bands[word]
print(word + ":", len(bands))
我認爲有一個更簡單的方法來做到這一點,但我不確定。另外,我不確定如何按頻率對字典進行排序,是否需要將其轉換爲列表?
查看['collections.Counter'](http://docs.python.org/2/library/collections.html#collections。計數器) –