使每个键在 Python 字典中最多存储 5 个值
Making each key store a maximum of 5 values in Python Dictionary
我最近一直在为我的同事创建一个猜谜游戏作为学习 Python 3.3x 的项目。我一直将结果存储在一个文本文件中,格式为名称和分数,用冒号分隔,如图所示...
Adam:12
Dave:25
Jack:13
Adam:34
Dave:23
使用以下代码读取文本文件,感谢 Padraic Cunningham。
from collections import defaultdict
d = defaultdict(list)
with open('guesses.txt') as f:
for line in f:
name,val = line.split(":")
d[name].append(int(val))
for k in sorted(d):
print(k," ".join(map(str,d[k])))
现在的问题是,我想查看 Dave、Adam 和 Jack 最近的四个分数。我想到的一种方法是以某种方式读取上面的列表并将其反转,以便它首先看到最新的结果。我想我可以先使用下面的代码行来反转字典:
inv_map = {v: k for k, v in d.items()}
但这不起作用,因为它 returns 错误:
TypeError: unhashable type: 'list'
因为我想存储 4 个最近的结果,所以我需要确保每次有新结果到达时删除最旧的结果,并更新字典。
我如何才能确保只为每个键分配了 4 个最大值?可以通过反转字典来完成吗?我试图查看其他问题是否遵循相同的原则,但我没有找到任何类似的东西。
注意 我已经看到了 itemgetter 方法,但每个键都有多个值。
文本文件将如下所示:
Adam:12
Dave:25
Jack:13
Adam:34
Dave:23
Jack:17
Adam:28
Adam:23
Dave:23
Jack:11
Adam:39
Dave:44
Jack:78
Dave:38
Jack:4
您可以使用 defaultdict
with deque(maxlen=4)
来处理。
import collections
d = collections.defaultdict(lambda: collections.deque(maxlen=4))
# defaultdict accepts as an argument a function that returns the default
# state of the value of undefined keys. In this case we make an anonymous
# function that returns a `collections.deque` with maxlen of 4.
# we could also do
# # import functools, collections
# # d = collections.defaultdict(functools.partial(collections.deque,
# # maxlen=4))
with open('path/to/file.txt', 'r') as infile:
for line in infile:
player,score = line.strip().split(":")
d[player].append(int(score))
但是,您最好先创建此数据结构并酸洗该对象。
import pickle
# `highscores` is some previously populated high score dict
def save_scores(filename):
with open(filename, 'w') as outfile:
pickle.dump(highscores, outfile)
def load_scores(filename):
with open(filename, 'r') as infile:
highscores = pickle.load(infile)
return highscores
我最近一直在为我的同事创建一个猜谜游戏作为学习 Python 3.3x 的项目。我一直将结果存储在一个文本文件中,格式为名称和分数,用冒号分隔,如图所示...
Adam:12
Dave:25
Jack:13
Adam:34
Dave:23
使用以下代码读取文本文件,感谢 Padraic Cunningham。
from collections import defaultdict
d = defaultdict(list)
with open('guesses.txt') as f:
for line in f:
name,val = line.split(":")
d[name].append(int(val))
for k in sorted(d):
print(k," ".join(map(str,d[k])))
现在的问题是,我想查看 Dave、Adam 和 Jack 最近的四个分数。我想到的一种方法是以某种方式读取上面的列表并将其反转,以便它首先看到最新的结果。我想我可以先使用下面的代码行来反转字典:
inv_map = {v: k for k, v in d.items()}
但这不起作用,因为它 returns 错误:
TypeError: unhashable type: 'list'
因为我想存储 4 个最近的结果,所以我需要确保每次有新结果到达时删除最旧的结果,并更新字典。
我如何才能确保只为每个键分配了 4 个最大值?可以通过反转字典来完成吗?我试图查看其他问题是否遵循相同的原则,但我没有找到任何类似的东西。
注意 我已经看到了 itemgetter 方法,但每个键都有多个值。
文本文件将如下所示:
Adam:12
Dave:25
Jack:13
Adam:34
Dave:23
Jack:17
Adam:28
Adam:23
Dave:23
Jack:11
Adam:39
Dave:44
Jack:78
Dave:38
Jack:4
您可以使用 defaultdict
with deque(maxlen=4)
来处理。
import collections
d = collections.defaultdict(lambda: collections.deque(maxlen=4))
# defaultdict accepts as an argument a function that returns the default
# state of the value of undefined keys. In this case we make an anonymous
# function that returns a `collections.deque` with maxlen of 4.
# we could also do
# # import functools, collections
# # d = collections.defaultdict(functools.partial(collections.deque,
# # maxlen=4))
with open('path/to/file.txt', 'r') as infile:
for line in infile:
player,score = line.strip().split(":")
d[player].append(int(score))
但是,您最好先创建此数据结构并酸洗该对象。
import pickle
# `highscores` is some previously populated high score dict
def save_scores(filename):
with open(filename, 'w') as outfile:
pickle.dump(highscores, outfile)
def load_scores(filename):
with open(filename, 'r') as infile:
highscores = pickle.load(infile)
return highscores