在python类中实现合并排序功能,错误 [英] Implementing merge sort function in python class, errors

查看:58
本文介绍了在python类中实现合并排序功能,错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我定义了一个函数,如果该函数是由线性数组实现的,则该函数非常适合对线性数组执行合并排序,但是如果将其放入类中,则会出错.我认为这是一个很好的例子,说明了我对类的工作方式不太了解.可能涉及名称空间管理(?).

So I have a function defined that works great at doing merge sort on a linear array if it is implemented by its lonesome, but if I put it into a class it bugs out. I think it's a great example of what I don't quite understand about how classes work; possibly in regards to namespace management(?).

参见下文:

def sort(array):
    print('Splitting', array)
    if len(array) > 1:
        m = len(array)//2
        left = array[:m]
        right = array[m:]

        sort(left)
        sort(right)

        i = 0
        j = 0
        k = 0

        while i < len(left) and j < len(right):
            if left[i] < right[j]:
                array[k] = left[i]
                i += 1
            else:
                array[k] = right[j]
                j += 1
            k += 1

        while i < len(left):
            array[k] = left[i]
            i += 1
            k += 1

        while j < len(right):
            array[k] = right[j]
            j += 1
            k += 1
    print('Merging', array)

arr = [1,6,5,2,10,8,7,4,3,9]
sort(arr)

产生预期的正确输出:

Splitting  [1, 6, 5, 2, 10, 8, 7, 4, 3, 9]
Splitting  [1, 6, 5, 2, 10]
Splitting  [1, 6]
Splitting  [1]
Merging  [1]
Splitting  [6]
Merging  [6]
Merging  [1, 6]
Splitting  [5, 2, 10]
Splitting  [5]
Merging  [5]
Splitting  [2, 10]
Splitting  [2]
Merging  [2]
Splitting  [10]
Merging  [10]
Merging  [2, 10]
Merging  [2, 5, 10]
Merging  [1, 2, 5, 6, 10]
Splitting  [8, 7, 4, 3, 9]
Splitting  [8, 7]
Splitting  [8]
Merging  [8]
Splitting  [7]
Merging  [7]
Merging  [7, 8]
Splitting  [4, 3, 9]
Splitting  [4]
Merging  [4]
Splitting  [3, 9]
Splitting  [3]
Merging  [3]
Splitting  [9]
Merging  [9]
Merging  [3, 9]
Merging  [3, 4, 9]
Merging  [3, 4, 7, 8, 9]
Merging  [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

但是,当我尝试在一个类中使用此函数时出现错误;我认为这与名称空间管理有关.见下文:

However, I get an error when I attempt to use this function in a class; something to do with namespace managment, I think. See below:

class MergeSort(object):

    def __init__(self, array):
        self.array = array

    def sort(self):
        print('Splitting', self.array)
        if len(self.array) > 1:
            m = len(self.array)//2
            left = self.array[:m]
            right = self.array[m:]

            sort(left)
            sort(right)

            i = 0
            j = 0
            k = 0

            while i < len(left) and j < len(right):
                if left[i] < right[j]:
                    self.array[k] = left[i]
                    i += 1
                else:
                    self.array[k] = right[j]
                    j += 1
                k += 1

            while i < len(left):
                self.array[k] = left[i]
                i += 1
                k += 1

            while j < len(right):
                self.array[k] = right[j]
                j += 1
                k += 1
        print('Merging', self.array)

x = MergeSort([1,6,5,2,10,8,7,4,3,9])
x.sort()

产生错误输出:

Splitting [1, 6, 5, 2, 10, 8, 7, 4, 3, 9]
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-15-89509f86277e> in <module>()
      1 x = MergeSort([1,6,5,2,10,8,7,4,3,9])
----> 2 x.sort()

<ipython-input-14-2bba116f00ce> in sort(self)
     11             right = self.array[m:]
     12 
---> 13             sort(left)
     14             sort(right)
     15 

NameError: name 'sort' is not defined

我最初的直觉是,在google搜索之后,通过添加前缀self.来更改子例程sort(left)和sort(right),但这会产生位置参数错误.对于我在这里不了解的内容,我想发表一两个评论.如果我的问题不是愚蠢的,那就为他们的好票而欢呼,如果我的问题不是愚蠢的话,那就为他们投反对票.

My initial instinct, after google searching around was to change subroutines sort(left) and sort(right) by adding prefixive self., but that generates a positional argument error. Would love a comment or two on what it is that I'm not understanding here. And cheers for good votes if my question is not stupid, and down votes if it is.

推荐答案

sort(left)不起作用的原因是,正如您推测的那样,如果不指定self,就不能在self上调用方法.不用说,它会寻找本地或全局名称sort,找不到名称,并引发NameError.

The reason sort(left) doesn't work is that, as you surmised, you can't call a method on self without specifying self. Leaving that off means it looks for a local or global name sort, doesn't find one, and raises a NameError.

self.sort(left)不起作用的原因是您定义的API不能那样工作.您的类将列表作为构造函数参数,然后将一个不带任何参数的sort应用于在构造时传入的列表.因此,您无法用其他数组调用自己的sort.如果尝试使用self.sort(left),则会传递错误数量的参数,就像调用abs(1, 2)一样,并且得到相同的TypeError.

The reason self.sort(left) doesn't work is that the API you defined doesn't work that way. Your class takes the list as a constructor argument, and then takes a sort with no arguments, that operates on the list passed in at construction time. So, you have no way to call your own sort with a different array. If you try self.sort(left), you're passing the wrong number of arguments, just like calling abs(1, 2), and you get the same TypeError.

您必须按设计方式使用API​​:使用新列表创建一个新的MergeSort排序器对象,然后在该新对象上调用sort:

You have to use your API the way you designed it: Create a new MergeSort sorter object with the new list, then call sort on that new object:

leftsorter = MergeSort(left)
leftsorter.sort()
rightsorter = MergeSort(right)
rightsorter.sort()

这篇关于在python类中实现合并排序功能,错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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