1 回答
TA贡献1786条经验 获得超13个赞
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | 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 |
添加回答
举报