我正在处理的问题要求我实施一个程序来显示平均带有avg的酒店。评级,稍后由用户检查。
https://www.dropbox.com/s/d7va219cqcdccsl/reviews.txt?dl=0个文件用于reviews.txt
Hotels.txt的https://www.dropbox.com/s/djbpox373l83m3b/hotels.txt?dl=0文件
下面代码中的第一个函数在第一次运行时给出我想要的结果,但是在以后的运行中返回2个空字典。
from google.colab import files
files.upload()
hotels = open("hotels.txt") # id \t hotel
reviews = open("reviews.txt") # user \t id \t rating
def hotelDicts(fileHotel, fileReview):
dictHotelID = {}
dictHotelPoint = {}
dictReview = {}
for line in fileHotel:
line = line.strip("\n").split("\t")
hotelID = line[0]
hotelName = line[1]
dictHotelID[hotelID] = hotelName
dictHotelPoint[hotelName] = [0, 0]
for line in fileReview:
line = line.strip("\n").split("\t")
user = line[0]
hotelID = line[1]
rating = int(line[2])
if hotelID in dictHotelID:
dictHotelPoint[dictHotelID[hotelID]][0] += rating
dictHotelPoint[dictHotelID[hotelID]][1] += 1
# print(dictHotelPoint[dictHotelID[hotelID]][0])
if user not in dictReview:
dictReview[user] = 1
elif user in dictReview:
dictReview[user] += 1
for hotel in dictHotelPoint:
total = dictHotelPoint[hotel][0]
count = dictHotelPoint[hotel][1]
if count > 0:
total = total / count
dictHotelPoint[hotel] = "%.1f"%total
elif count == 0:
dictHotelPoint[hotel] = 0
return dictHotelPoint, dictReview
def userHotel(dictHotel, dictReview):
maxUser = max(dictReview)
maxHotel = max(dictHotel)
print("The user who posted the most reviews is", maxUser)
print("The best hotel:", maxHotel)
while True:
hotelInput = input("Please enter a hotel name: ")
if hotelInput in dictHotel:
if dictHotel[hotelInput] != 0:
print("The average rating of the hotel:", dictHotel[hotelInput])
else:
print("Nobody has rated for this hotel yet.")
elif hotelInput not in dictHotel:
print("Hotel name does not exist in the database.")
elif hotelInput == "quit":
break
hotelDicts(hotels, reviews)
userHotel(hotelDicts(hotels, reviews)[0], hotelDicts(hotels, reviews)[1])
({{'Alila Manggis':'4.0', 'Aman':'3.0', “ Timeo贝尔蒙德大酒店”:0, “酿酒厂峡谷”:0, 'Esperanza':0, “绑架者角农场”:0, “四季度假村”:0, 'Hewing Hotel':'4.3', 'Hotel Maria Cristina':0, 'Katikies':'4.0', 'Pendry':'4.5', 'Planters Inn':'4.5', 'Qualia':'2.0', 'Sol y Luna':'2.0', '泰姬陵外交飞地':'5.0', 'The Beekman':0, “修道院”:0, 'The Lanesborough':'2.5', 'Setai':'2.0', 'Triple Creek Ranch':'3.0'}, {'carefreeted':3, 'dotingoutlaw':1, “兴奋剂携带者”:1, 'gargantuantrusty':2 'imaginaryscooner':1, 'kookybob':4 'noteworthymom':2 'preciouslegend':1, 'prejucidednorman':1, 'prejudicednorman':1, 'priceyscout':1, 'uniquehombre':1, 'yearlysecretariat':1})
列表酒店和评论的预期结果是这些。函数的第一次调用返回这些,但是在Google Colab上第二次运行时返回两个空字典。最后一行给出值错误:
ValueError: max() arg is an empty sequence
答案 0 :(得分:0)
首先,您可能不想调用这样的函数:
hotelDicts(hotels, reviews)
userHotel(hotelDicts(hotels, reviews)[0], hotelDicts(hotels, reviews)[1])
由于您正在执行的函数多次运行,因此最好这样调用:
dict1, dict2 = hotelDicts(hotels, reviews)
output = userHotel(dict1,dict2)
这有助于调试的一致性。
也在这里:
if hotelID in dictHotelID:
dictHotelPoint[dictHotelID[hotelID]][0] += rating
dictHotelPoint[dictHotelID[hotelID]][1] += 1
您可能想要dictHotelID后的“]”
以及将用户添加到字典的位置,可以使用dictionary.extend()方法:
a.extend(b)
这看起来也有点:
if count > 0:
total = total / count
dictHotelPoint[hotel] = "%.1f"%total
也许是:
if count > 0:
total = total / count
dictHotelPoint[hotel] = float(total)
我似乎看不到任何明显的原因,可能导致空序列,所以抱歉,我无法提供更多帮助。该线程似乎有更多信息: