2012-11-17 44 views
2

我有此信息的文本文件:匹配一組值到另外一個文本文件

1961 - Roger (Male) 
1962 - Roger (Male) 
1963 - Roger (Male) 
1963 - Jessica (Female) 
1964 - Jessica (Female) 
1965 - Jessica (Female) 
1966 - Jessica (Female) 

如果我要搜索文件中的單詞「羅傑」,我希望它打印在這個名字的相應年份,即1961年,1962年,1963年。對此,最好的辦法是什麼?

我用字典這樣做,但後來意識到後來的字典不能有重複的值和1963年在文本文件中被提及兩次,所以它沒有工作。

我使用Python 3,謝謝。

+0

還有什麼你試過嗎? – martineau

+0

使用一個'collections.defaultdict(list)',其中的關鍵是名字(可能是性別),並且年份被附加到相應的值,該值將自動從空列表開始。 – martineau

回答

2

使用字典的名稱作爲關鍵字並存儲多年的列表:

In [1]: with open("data1.txt") as f: 
    ...:  dic={} 
    ...:  for line in f: 
    ...:   spl=line.split() 
    ...:   dic.setdefault(spl[2],[]).append(int(spl[0])) 
    ...:  for name in dic :  
    ...:   print (name,dic[name]) 
    ...:  

Roger [1961, 1962, 1963] 
Jessica [1963, 1964, 1965, 1966] 

,或者您也可以使用collections.defaultdict

In [2]: from collections import defaultdict 

In [3]: with open("data1.txt") as f: 
    ...:  dic=defaultdict(list) 
    ...:  for line in f: 
    ...:   
    ...:   spl=line.split() 
    ...:   dic[spl[2]].append(int(spl[0])) 
    ...:  for name in dic:  
    ...:   print name,dic[name] 
    ...:   
Roger [1961, 1962, 1963] 
Jessica [1963, 1964, 1965, 1966] 
+0

你再一次對Ashwini有很大的幫助。它的作品,但是,我有實際的文件中的一些名稱,有中間名,所以spl [2]不會一直工作。我做了line.split(' - ')來解決這個問題,但它總是在每行的末尾產生一個「\ n」,爲什麼? – Goose

+0

@你可以使用'strip()',或簡單地'line.strip('\ n')。split(' - ')'來讀取'\ n'。 –

+0

得到*擺脫那 –

0

爲什麼你不能使用在名字的字典和索引(如Roger)爲重點,並有值的年(在這裏[1961,1962,1963]?列表是不是會爲你工作?

所以循環的你隨着年齡的增長uniquified作爲值的所有名稱是你彷彿想

+0

我試圖使用字典的方法,所以我有鑰匙作爲年,並作爲名稱的價值,當我搜索字典的值匹配「羅傑」它與1961年,1962年,但不是1963年,因爲傑西卡共享當年以及。 – Goose

+0

具有「羅傑」作爲鍵和「年」作爲值。那麼它會沒事的。 –

0

使用tuples可以將它們存儲在列表和遍歷

說你的名單看起來是這樣的。:

data = [(1961, 'Rodger', 'Male'), 
     (1962, 'Rodger', 'Male'), 
     (1963, 'Rodger', 'Male'), 
     (1963, 'Jessica', 'Female')] 

您可以像這樣運行的查詢就可以了:

​​

或者使用更Python代碼:

for year, name, sex in data: 
    if year >= 1962: 
     print "In {}, {} was {}".format(year, name, sex) 

1962年,羅傑是男
1963年,羅傑是男
1963年,傑西卡女

0

您可以隨時使用正則表達式。

import re 

f = open('names.txt') 
name = 'Roger' 

for line in f.readlines(): 
    match = re.search(r'([0-9]+) - %s' % name, line) 
    if match: 
     print match.group(1) 
0

正如我在評論中建議:

from collections import defaultdict 

result = defaultdict(list) 
with open('data.txt', 'rt') as input: 
    for line in input: 
     year, person = [item.strip() for item in line.split('-')] 
     result[person].append(year) 

for person, years in result.items(): 
    print(person, years, sep=': ') 

輸出:

Roger (Male): ['1961', '1962', '1963'] 
Jessica (Female): ['1963', '1964', '1965', '1966'] 
相關問題