繁体   English   中英

将文本文件中的一组值与另一组值匹配

[英]Matching one set of values to another in a text file

我有一个包含此信息的文本文件:

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

如果我想在文件中搜索“Roger”这个词,我希望它打印出该名称对应的年份,即 1961、1962、1963。解决这个问题的最佳方法是什么?

我是用字典做的,但后来意识到字典不能有重复的值,1963 在文本文件中被提及两次,所以它不起作用。

我正在使用 Python 3,谢谢。

使用以名称为键的字典并将年份存储在列表中:

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]

为什么你不能使用名称上的字典和索引(例如Roger )作为键,并将值作为年份列表(这里[1961,1962,1963] ?这对你不起作用吗?

所以在循环结束时,您将获得所有名称与年份统一的值,这正是您想要的。

使用元组 它们可以存储在列表中,并进行迭代。

假设您的列表如下所示:

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

您可以像这样对其运行查询:

# Just items where the name is Rodger
[(y, n, s) for y, n, s in data if n == "Rodger"]

# Just the year 1963
[(y, n, s) for y, n, s in data if y == 1963]

或者使用更多 Pythonic 代码:

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

1962年,罗杰是男性
1963年,罗杰是男性
1963年,杰西卡是女性

您始终可以使用正则表达式。

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)

正如我在评论中建议的那样:

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']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM