Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我如何制作按钮,将用户输入的数字按降序排序,因为它只按升序排序_Python_Sorting - Fatal编程技术网

Python 我如何制作按钮,将用户输入的数字按降序排序,因为它只按升序排序

Python 我如何制作按钮,将用户输入的数字按降序排序,因为它只按升序排序,python,sorting,Python,Sorting,因此,我的代码使用GUI进行选择排序,并希望添加一个按钮“排序降序”,将数字从最大到最小排序。谢谢,请帮忙!这是一个学校项目,因此功能更长等 #Variable of the array A = [] #Add Function def add(): global label_result newNumber = int(passwordEntry.get()) A.append(newNumber) label_result = tk.Label(master,

因此,我的代码使用GUI进行选择排序,并希望添加一个按钮“排序降序”,将数字从最大到最小排序。谢谢,请帮忙!这是一个学校项目,因此功能更长等

#Variable of the array
A = []
#Add Function
def add():
    global label_result
    newNumber = int(passwordEntry.get())
    A.append(newNumber)
    label_result = tk.Label(master, text=str(A))
    label_result.grid(row=3, column=0, columnspan=2)
#Sort Function
def sort(A):
    #Sets the comparison number - The book from demonstration
    for i in range(len(A)):
        # Find the minimum element in remaining
        # unsorted array
        minimum = i
        #The septre from demonstration (what its being compared to)
        for j in range(i+1, len(A)):
            #The stop sign (the lowest number out of the remainding numbers)
            if A[minimum] > A[j]:
                minimum = j

        # Swap the found minimum element with
        # the first element
        A[i], A[minimum] = A[minimum], A[i]
    label_msg['text'] = "Sorted"
    label_result['text'] = str(A)

master = tk.Tk()
master.title("Dans Selection Sort")
passwordEntry = tk.Entry(master)
passwordEntry.grid(row=0, column=1)
tk.Label(master, text='Enter Number: ', font='bold',).grid(row=0, column=0)
tk.Button(master, text='Add to Array', command=add).grid(row=1, column=0)
#lambda allows you to call a function with parameter.
tk.Button(master, text='Sort Ascending', command=lambda: sort(A)).grid(row=1, column=1)
tk.Button(master, text='Sort Descending', command=lambda: sort(A)).grid(row=1, column=2)
label_msg = tk.Label(master, text='')
label_msg.grid(row=2, column=0, columnspan=2)```
  • 您可以传入一个比较函数,即:
  • 大于λa,b:a>b

    def sort(A, compare):
       ...
       if compare(A[minimum], A[j]):
          ...
    
    更改比较函数以获得不同的顺序

  • 您还可以像标准函数一样传入反向布尔值:
  • 使用
    list.sort(reverse=True)

  • 使用
    list=sorted(list,reverse=True)


  • 嘿,谢谢你的回复,那么我是要做所有的还是只做一个?如果只有一行,我应该把“反向=错误”等放在哪一行。只有一行。如果您的练习要求您编写自己的排序,那么选项2将是最简单的。更改排序方法签名并将“if A[minimum]>A[j]:”行替换为答案中的一行。我把订单换成了asc(和往常一样,顺便说一句)。如果需要降序,则将调用从
    .sort(A)
    更改为
    .sort(A,reverse=True)
    A=[1,5,2]。A.排序(反向=真)。A=已排序(A,反向=真)。是啊,语法搞错了。这些是标准的排序函数,请参见。是的,你的排序函数不返回任何东西,所以你需要做排序(A),然后A=A[::-1]是的,你的排序(A)不返回任何东西。更新了我的答案。
    def sort(A, reverse=False):
      ...
      if (A[minimum] < A[j] and not reverse) or (not A[minimum] < A[j] and reverse):
         ...
    
    sort(A)
    A = A[::-1]