python阅读文件_在Python中阅读巨大的文件

我有一个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 to sentinel

编码二进制搜索求解(谢谢kigurai!):

entries = 24935502 # number of entries

width = 18 # fixed width of an entry in the file padded with spaces

# at the end of each line

for i, search in enumerate(list): # list contains the list of search keys

left, right = 0, entries-1

key = None

while key != search and left <= right:

mid = (left + right) / 2

fin.seek(mid * width)

key, value = map(int, fin.readline().split())

if search > key:

left = mid + 1

else:

right = mid - 1

if key != search:

value = None # for when search key is not found

search.result = value # store the result of the search

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值