我们有一个作业,我有一个严重的问题。关键是将每一行变成一个元组,并将这些元组变成一个列表。喜欢list=[tuple(line1),tuple(line2),tuple(line3),...]
。此外,还有很多用逗号分隔的字符串,比如"aei","1433","lincoln",...
这是问题:
一本书可以表示为作者的姓氏、作者的名字、标题、日期和 ISBN 的元组。
编写一个函数 ,
readBook()
给定一个包含此信息的逗号分隔字符串,返回一个表示该书的元组。编写一个函数 ,
readBooks()
给定包含每本书一个逗号分隔行的文本文件的名称,用于readBook()
返回一个元组列表,每个元组描述一本书。编写一个函数 ,
buildIndex()
给定由 返回的书籍列表readBooks()
,构建从关键字到书名的映射。关键词是书名中的任何单词,除了“a”、“an”或“the”。
这是我的代码:
RC=("Chann", "Robbbin", "Pride and Prejudice", "2013", "19960418")
RB=("Benjamin","Franklin","The Death of a Robin Thickle", "1725","4637284")
def readBook(lastName, firstName, booktitle, date, isbn):
booktuple=(lastName, firstName, booktitle, date, isbn)
return booktuple
# print readBook("Chen", "Robert", "Pride and Prejudice", "2013", "19960418")
def readBooks(file1):
inputFile = open(file1, "r")
lines = inputFile.readlines()
book = (lines)
inputFile.close()
return book
print readBooks("book.txt")
BooklistR=[RC,RB]
def buildIndex(file2):
inputFile= open("book.txt","r")
Blist = inputFile.readlines()
dictbooks={}
for bookinfo in Blist:
title=bookinfo[2].split()
for infos in title:
if infos.upper()=="A":
title.remove(infos)
elif infos.upper()=="THE":
title.remove(infos)
elif infos.upper()=="AN":
title.remove(infos)
else:
pass
dictbooks[tuple(title)]= bookinfo[2]
return dictbooks
print buildIndex("book.txt")
#Queries#
def lookupKeyword(keywords):
dictbooks=buildIndex(BooklistR)
keys=dictbooks.viewkeys()
values=dictbooks.viewvalues()
for keybook in list(keys):
for keyw in keywords:
for keyk in keybook:
if keyw== keyk:
printoo= dictbooks[keybook]
else:
pass
return printoo
print lookupKeyword("Robin")