URLをセットに格納します。これにより、アイテムを検索するためのO(1)が保証され、それが棚上げされます。この量のURLでは、保存と復元にかかる時間とメモリはごくわずかです。
import shelve
# Write URLS to shelve
urls= ['http://www.airmagnet.com/', 'http://www.alcatel-lucent.com/',
'http://www.ami.com/', 'http://www.apcc.com/', 'http://www.stk.com/',
'http://www.apani.com/', 'http://www.apple.com/',
'http://www.arcoide.com/', 'http://www.areca.com.tw/',
'http://www.argus-systems.com/', 'http://www.ariba.com/',
'http://www.asus.com.tw/']
s=set(urls) # Store URLs as set - Search is O(1)
sh=shelve.open('/tmp/shelve.tmp') # Dump set (as one unit) to shelve file
sh['urls']=s
sh.close()
sh=shelve.open('/tmp/shelve.tmp') # Retrieve set from file
s=sh['urls']
print 'http://www.apple.com/' in s # True
print 'http://matan.name/' in s # False
このアプローチは非常に高速です。
import random
import string
import shelve
import datetime
urls=[''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(50))
for i in range(40000)]
s=set(urls)
start=datetime.datetime.now()
sh=shelve.open('/tmp/test.shelve')
sh['urls']=urls
end=datetime.datetime.now()
print end-start