尝试在python中将权重排序到包中

2024-06-17 09:59:56 发布

您现在位置:Python中文网/ 问答频道 /正文

posted类似于这个问题,但是我在实现代码时遇到了问题。在

我有一个python程序,可以从HTML文件中收集数据,包括书籍的重量、价格、书名等。我想把书分成“n”个包,每个包不超过10磅。我可以运行程序没有错误,它提取的信息,但我没有得到任何结果包装。在

这是我的代码,谁能给我建议?在

import glob
from bs4 import BeautifulSoup

class weight():
        def main():
            data = []
            for filename in glob.iglob('*.html'):
                with open(filename) as f:
                    soup = BeautifulSoup(f)

                    weight = soup.find('b', text='Shipping Weight:').next_sibling
                    data.append({})

                    return weight 

        def Bin(weight):
            def __init__(self):
                self.items = []
                self.sum = 0

            def append(self, item):
                self.items.append(item)
                self.sum += item

            def __str__(self):
                return 'Bin(sum=%d, items=%s)' % (self.sum, str(self.items))

            def pack(values, maxValue):
                values = sorted(values, reverse=True)
                bins = []

                for item in values:
            # Try to fit item into a bin
                    for bin in bins:
                        if bin.sum + item <= maxValue:
                            #print 'Adding', item, 'to', bin
                            bin.append(item)
                            break
                else:
                # item didn't fit into any bin, start a new bin
                #print 'Making new bin for', item
                    Bin = weight()
                    bin.append(item)
                    bins.append(bin)

                return bins

if __name__ == '__main__':
    import random

    def packAndShow(aList, maxValue):
        print 'List with sum', sum(aList), 'requires at least', (sum(aList)+maxValue-1)/maxValue, 'bins'

        bins = pack(aList, maxValue)

        print 'Solution using', len(bins), 'bins:'
        for bin in bins:
            print bin

        print

    def pack(values, maxValue):
        values = sorted(values, reverse=True)
        bins = []

        for item in values:
            # Try to fit item into a bin
                    for bin in bins:
                        if bin.sum + item <= maxValue:
                            #print 'Adding', item, 'to', bin
                            bin.append(item)
                            break
        else:
                # item didn't fit into any bin, start a new bin
                #print 'Making new bin for', item
                    Bin = weight()
                    bin.append(item)
                    bins.append(bin)

        return bins

    if __name__ == '__main__':
        import random

        def packAndShow(aList, maxValue):
            print 'List with sum', sum(aList), 'requires at least', (sum(aList)+maxValue-1)/maxValue, 'bins'

            bins = pack(aList, maxValue)

            print 'Solution using', len(bins), 'bins:'
            for bin in bins:
                print bin

            print

Tags: inimportselfforbindefitemvalues
1条回答
网友
1楼 · 发布于 2024-06-17 09:59:56

你定义了类,但是你的代码布局从来没有实际运行过任何东西。在

把所有东西移到后面

if __name__ == "__main__": #(the first one)

在这样的函数下:

^{pr2}$

然后它会在调用时自动运行您的代码。在

我也会认真考虑重新调整你的代码。想得更小更简单 你在那些可以优化和消除的东西上浪费了大量的按键。比如定义append函数或创建从未使用过的类函数。在

我能给你的最好建议是尝试一些类似this的方法来更熟悉一些概念。它将消除许多简单的错误并使调试更容易。在

相关问题 更多 >