Python程序设计入门网易云课堂第七周作业调不出来了,不知道哪里错了,麻烦各位大神帮忙~~
我的代码:globalindexsindexs={}defreadindex():globalindexsmdict=[]foriinrange(1,101):line=...
我的代码:
global indexs
indexs = {}
def readindex():
global indexs
mdict = []
for i in range( 1, 101 ):
line = raw_input()
words = line.split( ' ' )
for word in words:
if word in indexs:
indexs[word].add( i )
else:
indexs[word] = set( [i] )
mdict.append( word )
mdict.sort();
for word in mdict:
line = ""
print word + ":",
lst = indexs[word]
for num in lst:
line += str( num ) + ", "
print line[:len( line ) - 2]
def query( s ):
global indexs
flag = 0
if s.startswith( "AND:" ):
flag = 0
s = s[4:]
elif s.startswith( "OR:" ):
flag = 1
s = s[3:]
lst = []
words = s.split( ' ' )
for word in words:
if word in indexs:
lst.append( indexs[word] )
else:
lst.append( set() )
mset = set()
if len( lst ) != 0:
mset = lst[0]
if flag == 0:
for item in lst:
mset = mset & item
else:
for item in lst:
mset = mset | item
line = ""
if len( mset ) == 0:
print "None"
else:
for item in mset:
line += str( item ) + ", "
print line[:len( line ) - 2]
readindex()
while True:
s = raw_input()
if s == "":
break;
query( s )
感谢各位的帮助~ 展开
global indexs
indexs = {}
def readindex():
global indexs
mdict = []
for i in range( 1, 101 ):
line = raw_input()
words = line.split( ' ' )
for word in words:
if word in indexs:
indexs[word].add( i )
else:
indexs[word] = set( [i] )
mdict.append( word )
mdict.sort();
for word in mdict:
line = ""
print word + ":",
lst = indexs[word]
for num in lst:
line += str( num ) + ", "
print line[:len( line ) - 2]
def query( s ):
global indexs
flag = 0
if s.startswith( "AND:" ):
flag = 0
s = s[4:]
elif s.startswith( "OR:" ):
flag = 1
s = s[3:]
lst = []
words = s.split( ' ' )
for word in words:
if word in indexs:
lst.append( indexs[word] )
else:
lst.append( set() )
mset = set()
if len( lst ) != 0:
mset = lst[0]
if flag == 0:
for item in lst:
mset = mset & item
else:
for item in lst:
mset = mset | item
line = ""
if len( mset ) == 0:
print "None"
else:
for item in mset:
line += str( item ) + ", "
print line[:len( line ) - 2]
readindex()
while True:
s = raw_input()
if s == "":
break;
query( s )
感谢各位的帮助~ 展开
展开全部
w_dic = {}
for i in range(1,101):
line = raw_input()
for w in line.split():
if w in w_dic:
if not (i in w_dic[w]):
w_dic[w].append(i)
else:
w_dic[w] = [i]
sr = []
while True:
qs = raw_input()
if qs == '':
break
sr.append(qs)
jh = w_dic.keys()
jh.sort()
for w in jh:
print w+': '+str(w_dic[w])[1:-1]
for qs in sr:
os = []
if qs[0:3] == 'OR:':
ws = qs[3:].split()
for w in ws:
if w in w_dic:
os = list(set(os) | set(w_dic[w]))
if len(os) == 0:
print 'None'
else:
os.sort()
print str(os)[1:-1]
else:
if qs[0:4] == 'AND:':
qs = qs[4:]
ws = qs.split()
if ws[0] in w_dic:
os = w_dic[ws[0]]
for w in ws:
if w in w_dic:
os = list(set(os) & set(w_dic[w]))
else:
os = []
break
if len(os) == 0:
print 'None'
else:
os.sort()
print str(os)[1:-1]
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询