文本文件可能有重复项,这将覆盖字典中现有的键(哈希表的python名称)。您可以创建一组唯一的键,然后使用字典理解来填充字典。在
样本_文件.txta
b
c
c
Python代码
^{pr2}$
这是一个包含100万个长度为10的随机字母字符的实现。时间安排在半秒以下是相对微不足道的。在import string
import numpy as np
letter_map = {n: letter for n, letter in enumerate(string.ascii_lowercase, 1)}
long_alpha_list = ["".join([letter_map[number] for number in row]) + "\n"
for row in np.random.random_integers(1, 26, (1000000, 10))]
>>> long_alpha_list[:5]
['mfeeidurfc\n',
'njbfzpunzi\n',
'yrazcjnegf\n',
'wpuxpaqhhs\n',
'fpncybprrn\n']
>>> len(long_alpha_list)
1000000
# Write list to file.
with open('sample_file.txt', 'wb') as f:
f.writelines(long_alpha_list)
# Read them back into a dictionary per the method above.
with open("sample_file.txt") as f:
keys = set(line.strip() for line in f.readlines())
>>> %%timeit -n 10
>>> my_dict = {key: 1 for key in keys if key}
10 loops, best of 3: 379 ms per loop