python单词倒排_python倒排索引(Inverted index)

展开全部

s = raw_input()

lines = s.split('\n')

dictlines = lines[:100]

mydict = {}

# read

for i,line in enumerate(dictlines ):

for word in line.split():

mydict.setdefault(word,[]).append(i + 1)

# print indices

for word in mydict.keys():

print "%s: %s" % (word,", ".join(map(str,sorted(mydict[word]))))

def andSearch(words_list):

global mydict

a = set(range(1,101))

for word in words_list:

a = a.intersection(set(mydict[word]))

return a

def orSearch(words_list):

global mydict

a = set([])

for word in words_list:

a = a.union(set(mydict[word]))

return a

# Query

index = 100

u = lines[index]

while index < len(lines):

words_list = u.split()

if ":" in u:

if words_list[0] == "OR:":

a = orSearch(words_list)

else:

if words_list[0] == 'AND:':

words_list = words_list[1:]

a = andSearch(words_list)

if not a:

print ", ".join(map(str,list(a)))

else:

print "None"

index += 1

大致思想就是这e68a8462616964757a686964616f31333337393536样。。。。。。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值