试图重排序成Python包 [英] Attempting to sort weight into packages in python

查看:179
本文介绍了试图重排序成Python包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

posted类似这样的问题的东西,但我在执行code麻烦。

我有一个从包括重量,价格,标题我想没有每个包超过10磅的书籍为N包排序书籍等的HTML文件收集数据的Python程序。我可以运行没有错误的程序,并提取信息,但我不明白的包装任何结果。

下面是code,我有,任何人都可以给我建议?

 进口水珠
从BS4进口BeautifulSoup

类权重():
        高清的main():
            数据= []
            为文件名的glob.iglob(* HTML):
                开放(文件名)为f:
                    汤= BeautifulSoup(F)

                    体重= soup.find('B',文本='重量:')。NEXT_SIBLING
                    data.append({})

                    返回重

        高清斌(重量):
            高清__init __(个体经营):
                self.items = []
                self.sum = 0

            高清追加(个体经营,项目):
                self.items.append(项目)
                self.sum + =项目

            高清__str __(个体经营):
                回归斌(总和=%D,项目=%S)'%(self.sum,STR(self.items))

            高清包(价值,包括maxValue):
                值=排序(值,反向=真)
                箱= []

                在值项:
            #尝试,以适应项目成仓
                    为斌在垃圾桶:
                        如果bin.sum +项目< = maxValue(最大值):
                            #PRINT'添加',项目,'到',仓
                            bin.append(项目)
                            打破
                其他:
                #项目没有融入任何斌,开始一个新宾
                #PRINT让新仓,项目
                    宾=体重()
                    bin.append(项目)
                    bins.append(斌)

                返回箱

如果__name__ =='__main__':
    进口随机

    高清packAndShow(ALIST,包括maxValue):
        打印列出的额头,和(ALIST),至少需要',(和(ALIST)+ maxValue分别-1)/ maxValue(最大值),垃圾桶

        箱=包(ALIST,包括maxValue)

        ,LEN(箱),打印使用解决方案''垃圾桶:
        为斌在垃圾桶:
            印斌

        打印

    高清包(价值,包括maxValue):
        值=排序(值,反向=真)
        箱= []

        在值项:
            #尝试,以适应项目成仓
                    为斌在垃圾桶:
                        如果bin.sum +项目< = maxValue(最大值):
                            #PRINT'添加',项目,'到',仓
                            bin.append(项目)
                            打破
        其他:
                #项目没有融入任何斌,开始一个新宾
                #PRINT让新仓,项目
                    宾=体重()
                    bin.append(项目)
                    bins.append(斌)

        返回箱

    如果__name__ =='__main__':
        进口随机

        高清packAndShow(ALIST,包括maxValue):
            打印列出的额头,和(ALIST),至少需要',(和(ALIST)+ maxValue分别-1)/ maxValue(最大值),垃圾桶

            箱=包(ALIST,包括maxValue)

            ,LEN(箱),打印使用解决方案''垃圾桶:
            为斌在垃圾桶:
                印斌

            打印
 

解决方案

您定义的类,但你的code中的布局从来没有真正运行任何东西。

动的一切

 如果__name__ ==__main__:#(第一个)
 

像这样一个作用下才:

 高清的main():
    从下面粘贴数据
 

然后,它会自动在调用它运行code。

此外,我会认真考虑重新调整你的code。想想小和更简单 你浪费了大量的按键上的东西,可以优化和消除。就像定义一个附加的功能或者创建从未使用过类功能。

我可以给你最好的建议是尝试一些像,以获得更多的熟悉的一些概念。这将消除大量的简单错误,使调试更加容易。

I posted something similar to this question but I'm having trouble implementing the code.

I have a python program that collects data from HTML files that includes the weight, price, title of books etc. I want to sort the books into "n" packages without each package exceeding 10 pounds. I can run the program without errors and it extracts the information but I do not get any results on the packing.

Here is the code that I have, can anyone give me suggestions?

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

解决方案

You define the class but the layout of your code never actually runs anything.

move everything after

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

BEFORE it under a function like so:

def main():
    paste the data from below

Then it will automatically run your code upon calling it.

Also I would seriously look into refocusing your code. Think smaller and simpler you're wasting a lot of keystrokes on things that could be optimized and eliminated. like defining an append function or creating class functions that are never used.

Best advice I can give you is to try something like this to get more familiar with some of the concepts. It'll eliminate a lot of the simple errors and make the debugging easier.

这篇关于试图重排序成Python包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆