有没有一种有效的方法可以从一组产品中找到最低的价格?

时间:2019-04-13 16:43:18

标签: python

我有我想要的产品的购物清单以及来自不同供应商的产品清单。 并非每个供应商都拥有我想要的所有产品,向任何供应商订购都意味着要收取运费。

我有一个您肯定要购买的产品清单;并希望从我要选择供应商的角度考虑最低价格,以使该价格对于该产品列表而言最低。

我想购买Magic the Gathering Boosters,但没有一个供应商将所有这些都放在mkm上。因此,我从销售我想要的任何一种助推器的所有供应商处下载了所有产品的列表。现在,我有一个包含577种产品的sqlite数据库,但不知道如何找到最便宜的供应商和增强器组合。我尝试了以下代码,但遇到内存问题。有办法更有效地解决这个问题吗?

课程:

class Booster:

    def __init__(self, booster_id, seller, price, country):
        self.booster_id = booster_id
        self.seller = seller
        self.price = price
        self.country = country


class BoosterList:

    def __init__(self, sellers, boosters, price):
        self.sellers = sellers
        self.price = price
        self.boosters = boosters

    def add_booster(self, booster):
        """creates a new list of boosters with a list of all past boosters"""
        self.price_new = self.price + booster.price
        self.boosters_new = self.boosters + [booster]
        if booster.seller not in self.sellers:
            self.sellers_new = self.sellers + [booster.seller]
            self.price_new += shipping[booster.country]
        else:
            self.sellers_new = self.sellers
            self.price_new = self.price
        return BoosterList(self.sellers_new, self.boosters_new, self.price_new)

和搜索算法:

    def find_min_price(self):
        db = sqlite3.connect(self.db_location)
        c = db.cursor()

        booster_list_list = [BoosterList([], [], 0)]
        self.booster_list_list_new = []


        #finds the booster_ids of all the wanted boosters
        booster_ids = c.execute('select product_id from booster_info').fetchall()


        #finds all the products for all the boosters_id where vendors have more than 5 different boosters
        for booster_id in booster_ids[16:23]:
            print(booster_id, len(booster_ids))
            articles = c.execute("""select * from articles where
                                 product_id = ? and user_id in
                                (select user_id from articles 
                                 where country is not 'CH'
                                 group by user_id 
                                 having count(user_id)>5
                                 order by count(user_id)) """, booster_id).fetchall()

            for article in articles:
                booster_id = article[0]
                price = article[2]
                user_id = article[3]
                country = article[5]
                booster = Booster(booster_id, user_id, price, country)

                #adds every found booster to the booster list/creates a new one 
                for booster_list in booster_list_list:
                    self.booster_list_list_new.append(booster_list.add_booster(booster))
            booster_list_list = self.booster_list_list_new
            self.booster_list_list_new = []

        db.close()
        return booster_list_list

运行此代码时,它的工作原理很吸引人,但是需要太多内存。有没有找到最低价格的更有效内存的方法?

1 个答案:

答案 0 :(得分:0)

我使用骗子的想法,将供应商的数量减少到4个,然后运行算法。现在,我有一个解决方案,希望它接近最低要求。