首页 > 解决方案 > 旅行社类

问题描述

旅行社类

大家好,我正在研究使用 Python 的推荐引擎项目。

这是代码:

destinations = ["Paris, France", "Shanghai, China", "Los Angeles, USA", "São Paulo, Brazil", "Cairo, Egypt"]
test_traveler = ['Erin Wilkes', 'Shanghai, China', ['historical site', 'art']]
attractions = [[], [], [], [], []]

def get_destination_index(destination):
  destination_index = destinations.index(destination)
  return(destination_index)
#print(get_destination_index("Los Angeles, USA"))
#get_destination_index("Hyderabad, India")

def get_traveler_location(traveler):
  traveler_destination = traveler[1]
  traveler_destination_index = get_destination_index(traveler_destination)
  return(traveler_destination_index) 
attractions = [[] for attraction in destinations]
#print(attractions)

def add_attraction(destination, attraction):
  destination_index = get_destination_index(destination)
  try:
    destination_index = get_destination_index(destination)
    attractions_for_destination = attractions[destination_index]
    attractions_for_destination.append(attraction)
  except SyntaxError:
    return
  attractions_for_destination = attractions[destination_index]

def find_attractions(destination, interests):
  destination_index = get_destination_index(destination)
  attractions_in_city = attractions[destination_index]
  attractions_with_interest = []
  for attractions in attractions_in_city:
    possible_attraction = attractions
    attraction_tags = attractions[1]
    for interest in interests:
      if interest in attraction_tags:
        attractions_with_interest
    

test_destination_index = get_traveler_location(test_traveler)
#print(add_attraction("Los Angeles, USA", ['Venice Beach', ['beach']]
#))
add_attraction("Paris, France", ["the Louvre", ["art", "museum"]])
add_attraction("Paris, France", ["Arc de Triomphe", ["historical site", "monument"]])
add_attraction("Shanghai, China", ["Yu Garden", ["garden", "historcical site"]])
add_attraction("Shanghai, China", ["Yuz Museum", ["art", "museum"]])
add_attraction("Shanghai, China", ["Oriental Pearl Tower", ["skyscraper", "viewing deck"]])
add_attraction("Los Angeles, USA", ["LACMA", ["art", "museum"]])
add_attraction("São Paulo, Brazil", ["São Paulo Zoo", ["zoo"]])
add_attraction("São Paulo, Brazil", ["Pátio do Colégio", ["historical site"]])
add_attraction("Cairo, Egypt", ["Pyramids of Giza", ["monument", "historical site"]])
add_attraction("Cairo, Egypt", ["Egyptian Museum", ["museum"]])
#print(attractions)


def find_attractions(destination, interests):
    destination_index = get_destination_index(destination)
    attractions_in_city = attractions[destination_index]
    attractions_with_interest = []
    for attraction in attractions_in_city:
        possible_attraction = attraction
        attraction_tags = attraction[1]
        for interest in interests:
            if interest in attraction_tags:
                attractions_with_interest.append(possible_attraction[0])
    return attractions_with_interest


#la_arts = find_attractions("Los Angeles, USA", ['art'])
#print(la_arts)



#smills_france = get_attractions_for_traveler(['Kanav Baid', 'Shanghai, China'], ['art', 'historcical site'])
#print(smills_france)
def get_attractions_for_traveler(traveler, interests):
  traveler_destination = traveler[1]
  traveler_interests = interests
  traveler_attractions = find_attractions(traveler_destination, traveler_interests)
  interests_string = "Hi " + traveler[0] + ", we think you'll like these places around " + traveler_destination + ":"
  for element in traveler_attractions:
    interests_string += " the " + element + ","
  return interests_string



Kanav = get_attractions_for_traveler(['Kanav Baid', 'Shanghai, China'], ['art', 'historcical site'])
print(Kanav)

在这里,您可以看到显示输出,我需要手动创建一个变量,我正在考虑如何使用类来完成它,我尝试过,但它一直发送错误。它如何工作的一个例子是这样的

Daniel = Traveler(['Daniel', 'Shanghai, China'], ['art', 'historcical site'])

标签: pythonclassoop

解决方案


我不知道我是否理解你需要什么,但这是你的代码重写为一个类:

class Traveler(object):
    def __init__(self, destination, traveler):
        self.destinations = destination
        self.test_traveler = traveler
        self.attractions = [[] for attraction in self.destinations]

    def get_destination_index(self, destination):
        destination_index = self.destinations.index(destination)
        return (destination_index)

    def get_traveler_location(self, traveler):
        traveler_destination = traveler[1]
        traveler_destination_index = self.get_destination_index(traveler_destination)
        return (traveler_destination_index)

    def add_attraction(self, destination, attraction):
        self.destination_index = self.get_destination_index(destination)
        try:
            destination_index = self.get_destination_index(destination)
            self.attractions_for_destination = self.attractions[destination_index]
            self.attractions_for_destination.append(attraction)
        except SyntaxError:
            return
        self.attractions_for_destination = self.attractions[destination_index]

    def find_attractions(self, destination, interests):
        destination_index = self.get_destination_index(destination)
        attractions_in_city = self.attractions[destination_index]
        attractions_with_interest = []
        for attractions in attractions_in_city:
            possible_attraction = attractions
            attraction_tags = attractions[1]
            for interest in interests:
                if interest in attraction_tags:
                    attractions_with_interest

    def find_attractions(self, destination, interests):
        destination_index = self.get_destination_index(destination)
        attractions_in_city = self.attractions[destination_index]
        attractions_with_interest = []
        for attraction in attractions_in_city:
            possible_attraction = attraction
            attraction_tags = attraction[1]
            for interest in interests:
                if interest in attraction_tags:
                    attractions_with_interest.append(possible_attraction[0])
        return attractions_with_interest

    def get_attractions_for_traveler(self, traveler, interests):
        traveler_destination = traveler[1]
        traveler_interests = interests
        traveler_attractions = self.find_attractions(traveler_destination, traveler_interests)
        interests_string = "Hi " + traveler[0] + ", we think you'll like these places around " + traveler_destination + ":"
        for element in traveler_attractions:
            interests_string += " the " + element + ","
        return interests_string

    def get_destination_index(self, destination):
        destination_index = self.destinations.index(destination)
        return (destination_index)

    def get_traveler_location(self, traveler):
        traveler_destination = traveler[1]
        traveler_destination_index = self.get_destination_index(traveler_destination)
        return (traveler_destination_index)

    def add_attraction(self, destination, attraction):
        self.destination_index = self.get_destination_index(destination)
        try:
            self.destination_index = self.get_destination_index(destination)
            self.attractions_for_destination = self.attractions[self.destination_index]
            self.attractions_for_destination.append(attraction)
        except SyntaxError:
            return
        self.attractions_for_destination = self.attractions[self.destination_index]

    def find_attractions(self, destination, interests):
        destination_index = self.get_destination_index(destination)
        attractions_in_city = self.attractions[destination_index]
        attractions_with_interest = []
        for attractions in attractions_in_city:
            possible_attraction = attractions
            attraction_tags = attractions[1]
            for interest in interests:
                if interest in attraction_tags:
                    attractions_with_interest

    def find_attractions(self, destination, interests):
        destination_index = self.get_destination_index(destination)
        attractions_in_city = self.attractions[destination_index]
        attractions_with_interest = []
        for attraction in attractions_in_city:
            possible_attraction = attraction
            attraction_tags = attraction[1]
            for interest in interests:
                if interest in attraction_tags:
                    attractions_with_interest.append(possible_attraction[0])
        return attractions_with_interest

    def get_attractions_for_traveler(self, traveler, interests):
        traveler_destination = traveler[1]
        traveler_interests = interests
        traveler_attractions = self.find_attractions(traveler_destination, traveler_interests)
        interests_string = "Hi " + traveler[0] + ", we think you'll like these places around " + traveler_destination + ":"
        for element in traveler_attractions:
            interests_string += " the " + element + ","
        return interests_string

然后你可以这样使用:

Daniel = Traveler(["Paris, France", "Shanghai, China", "Los Angeles, USA", "São Paulo, Brazil", "Cairo, Egypt"], ['Erin Wilkes', 'Shanghai, China', ['historical site', 'art']])

推荐阅读