我有一个384MB的文本文件,有5000万行.每行包含2个以空格分隔的整数:键和值.该文件按键排序.我需要一种有效的方法来查找Python中大约200个键列表的值.
我目前的方法包括在下面.这需要30秒.必须有更高效的Python foo才能将其降低到最多几秒钟的合理效率.
# list contains a sorted list of the keys we need to lookup
# there is a sentinel at the end of list to simplify the code
# we use pointer to iterate through the list of keys
for line in fin:
line = map(int, line.split())
while line[0] == list[pointer].key:
list[pointer].value = line[1]
pointer += 1
while line[0] > list[pointer].key:
pointer += 1
if pointer >= len(list) - 1:
break # end of list; -1 is due …Run Code Online (Sandbox Code Playgroud)