简体   繁体   English

python class 内的合并排序

[英]python merge sort within a class

I am trying to make merge sort work within a class "Sorter for a python project, using the first code. The problem is that whenever I initialize the code, it calls the error "merge_sort' is not defined", and if I remove the "merge_sort" and use "left = lst[:mid]", it only cuts the list into half & reorganizes, but doesn't complete the script with the whole list. Is there a way to get around this issue? Thanks!!我正在尝试使用第一个代码在 class “python 项目的排序器”中进行合并排序。问题是,每当我初始化代码时,它都会调用错误“merge_sort'未定义”,如果我删除“merge_sort”并使用“left = lst[:mid]”,它只会将列表切成两半并重新组织,但不会用整个列表完成脚本。有没有办法解决这个问题?谢谢!

from sorter import Sorter
unsorted_list = [5, -3, 4, 10, -14, 2, 4, -5]
my_sorter = Sorter()
my_sorter.unsorted_tuple = tuple(unsorted_list)
sorted_list = my_sorter._merge_sort()
print(sorted_list)

My code:我的代码:

class Sorter():

    def __init__(self):
        self.unsorted_tuple = tuple([])
        self.algorithm = 'default'

    def generate_new_tuple(self, n):

        new_list = []
        for x in range (0, n):
            new_list.append(random.randint(0, maxsize))
        tuple(new_list)
        self.unsorted_tuple = new_list
        return None

    def _merge_sort(self, reverse=False):

        lst = list(self.unsorted_tuple)
        result = []
        i,j = 0,0

        if(len(lst)<= 1): 
            return lst
        mid = int(len(lst)/2)
        left = _merge_sort(lst[:mid])
        right = _merge_sort(lst[mid:])

        while i<len(left) and j<len(right):
            if left[i] <= right[j]:
                result.append(left[i]) 
                i+=1
            else:
                result.append(right[j])
                j+=1
        result += left[i:]
        result += right[j:]
        return result

You're confused about how classes and methods work.您对类和方法的工作方式感到困惑。

The compiler is correct (by definition...): there is no function _merge_sort .编译器是正确的(根据定义......):没有 function _merge_sort That name applies to a method, and must be called with a Sorter object.该名称适用于方法,并且必须使用Sorter object 调用。 You have gone to a lot of trouble to set up this class, but then you've ignored those encapsulation protections when you try to recur on the halves of your list:您在设置此 class 时遇到了很多麻烦,但是当您尝试在列表的一半上重复时,您忽略了这些封装保护:

    left = _merge_sort(lst[:mid])
    right = _merge_sort(lst[mid:])

You're trying to invoke your method as if it were a common function.您正在尝试调用您的方法,就好像它是一个常见的 function。 Instead, you have to instantiate Sorter objects for each half of the list, set their unsorted attributes to those half-lists, and then you can invoke the method on each of those.相反,您必须为列表的每一半实例化Sorter对象,将它们的未排序属性设置为这些半列表,然后您可以在每个列表上调用该方法。 Something like:就像是:

left_half = Sorter()
left_half.unsorted_tuple = lst[:mid]
left = left_half._merge_sort()
right_half = Sorter()
right_half.unsorted_tuple = lst[mid:]
right = right_half._merge_sort()

Please consider the "weight" of this code;请考虑此代码的“重量”; perhaps you can reconfigure your class to better support your needs.也许您可以重新配置您的 class 以更好地满足您的需求。 For starters, give __init__ another parameter for the initial value of the list/tuple.对于初学者,给__init__另一个参数作为列表/元组的初始值。

Does that get you moving?这会让你感动吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM