-5

次のテキストを含むファイルがあります

1. Beatles - Revolver (1966)
2. Nirvana - Nevermind (1991)
3. Beatles - Sgt Pepper's Lonely Hearts Club Band (1967)
4. U2 - The Joshua Tree (1987)
5. Beatles - The Beatles (The White Album) (1968)
6. Beatles - Abbey Road (1969)
7. Guns N' Roses - Appetite For Destruction (1987)
8. Radiohead - Ok Computer (1997)
9. Led Zeppelin - Led Zeppelin 4 (1971)
10. U2 - Achtung Baby (1991)
11. Pink Floyd - Dark Side Of The Moon (1973)
12. Michael Jackson -Thriller (1982)
13. Rolling Stones - Exile On Main Street (1972)
14. Clash - London Calling (1979)
15. U2 - All That You Can't Leave Behind (2000)
16. Weezer - Pinkerton (1996)
17. Radiohead - The Bends (1995)
18. Smashing Pumpkins - Mellon Collie And The Infinite Sadness (1995)
19. Pearl Jam - Ten (1991)
20. Beach Boys - Pet Sounds (1966)
21. Weezer - Weezer (1994)
22. Nirvana - In Utero (1993)
23. Beatles - Rubber Soul (1965)
24. Eminem -The Eminem Show (2002)
25. R.E.M. - Automatic For The People (1992)
26. Radiohead - Kid A (2000)
27. Tool - Aenima (1996)
28. Smashing Pumpkins - Siamese Dream (1993)
29. Madonna - Ray Of Light (1998)
30. Rolling Stones - Sticky Fingers (1971)
...till line 99.

そのため、キーがバンド名であるディクショナリに情報を保存する必要があり、関連する値はそのバンドのすべてのベスト アルバムを含むリストです。このリストの各エントリは、アルバム名とリリースされた年の 2 つのフィールドで構成されるタプルです。句読点と括弧も削除する必要があります。誰か助けてくれませんか?

4

3 に答える 3

2

手始めにこれを試してください。これは完全ではありません。ここから取得して、必要に応じて微調整する必要があります。

import re

my_dict = {}
for record in songs:
    year = re.findall('\(([0-9]{4})\)', record)
    band = re.findall('[0-9]+\. (.*)', l.split('-')[0])
    song = re.findall('(.*) \(', record.split('-')[1].strip())

    if song and band and year:
        if my_dict.has_key(band): #alread present, append 
            my_dict[band].append((song, year))
        else: #create new entry
            my_dict[band] = [(song, year)]

print my_dict
于 2013-04-27T03:15:25.630 に答える
1

これがあなたにとってよりうまくいくかもしれない解決策です:

import re
from collections import defaultdict

band_dict = defaultdict(list)
pattern   = re.compile(r"\d+\. (?P<band>.+?) -\s?(?P<album>.+?) \((?P<year>\d+)\)")
with open("musiclist") as f:
    for line in f:
        match = pattern.match(line)
        if match:
            groupdict = match.groupdict()
            band_dict[groupdict['band']].append((groupdict['album'], groupdict['year']))
        else:
            print "Error, no match for line %s" % line

for band in band_dict:
    print band
    for album, year in band_dict[band]:
        print "\t%s: %s" % (album, year)

提供したデータでこれを実行すると、次のようになりmusiclistます

Pink Floyd
    Dark Side Of The Moon: 1973
Beatles
    Revolver: 1966
    Sgt Pepper's Lonely Hearts Club Band: 1967
    The Beatles (The White Album): 1968
    Abbey Road: 1969
    Rubber Soul: 1965
Clash
    London Calling: 1979
Rolling Stones
    Exile On Main Street: 1972
    Sticky Fingers: 1971
Led Zeppelin
    Led Zeppelin 4: 1971
R.E.M.
    Automatic For The People: 1992
Guns N' Roses
    Appetite For Destruction: 1987
U2
    The Joshua Tree: 1987
    Achtung Baby: 1991
    All That You Can't Leave Behind: 2000
Nirvana
    Nevermind: 1991
    In Utero: 1993
Pearl Jam
    Ten: 1991
Tool
    Aenima: 1996
Beach Boys
    Pet Sounds: 1966
Madonna
    Ray Of Light: 1998
Radiohead
    Ok Computer: 1997
    The Bends: 1995
    Kid A: 2000
Eminem
    The Eminem Show: 2002
Weezer
    Pinkerton: 1996
    Weezer: 1994
Smashing Pumpkins
    Mellon Collie And The Infinite Sadness: 1995
    Siamese Dream: 1993
Michael Jackson
    Thriller: 1982
于 2013-04-27T04:30:18.873 に答える
1

私がすることは、ファイルから各行を読み取り、それを文字列に解析し、文字列をごとに分割し.、最初の文字列をキーに、2 番目の文字列を値にすることです。元:

albumDict = {}
file = open(/path/to/file, "r")
for line in file.readlines():
    splitLine = line.split(".")
    albumDict[splitLine[0]] = splitline[1]

編集: 注: これは重複したエントリをチェックしません。また、専門的な設定で実装するべきではありません。これを複数の人が使用できるようにする場合は、キーがまだ存在しないことを確認するチェックを追加します。

于 2013-04-27T03:17:43.560 に答える