나는 밴드 목록과 제작 된 앨범 및 연도가있는 파일을 가지고 있습니다. 이 파일을 살펴보고 다른 이름의 밴드를 찾고 해당 밴드가이 파일에 몇 번 나왔는지 계산하는 함수를 작성해야합니다. 파일이 보이는특정 단어를 가져 와서 각 단어/단어의 빈도를 인쇄 하시겠습니까?
방법은 다음과 같이이다 : 여기
band1: number1
band2: number2
band3: number3
내가 지금까지 가지고있는 코드 :
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)
.
.
.
출력 주파수의 내림차순으로하고 다음과 같이한다 :
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. 카운터) –