Function 用于冒泡排序2个列表的单个函数

Function 用于冒泡排序2个列表的单个函数,function,python-3.x,bubble-sort,Function,Python 3.x,Bubble Sort,晚上好。我已经设法把清单一列出来了。列表二也需要排序。是否有一种方法可以将listTwo添加到我已有的冒泡排序中,从而也可以对其进行排序。 还是我需要写另一个循环 listOne = [3, 9, 2, 6, 1] listTwo = [4, 8, 5, 7, 0] def bubbleSort (inList): moreSwaps = True while (moreSwaps): moreSwaps = False for element

晚上好。我已经设法把清单一列出来了。列表二也需要排序。是否有一种方法可以将listTwo添加到我已有的冒泡排序中,从而也可以对其进行排序。 还是我需要写另一个循环

    listOne = [3, 9, 2, 6, 1]
    listTwo = [4, 8, 5, 7, 0]

    def bubbleSort (inList):

    moreSwaps = True
while (moreSwaps):
    moreSwaps = False
    for element in range(len(listOne)-1):
        if listOne[element]> listOne[element+1]:
            moreSwaps = True
            temp = listOne[element]
            listOne[element]=listOne[element+1]
            listOne[element+1]= temp
return (inList)

      print ("List One = ", listOne)
      print ("List One Sorted = ", bubbleSort (listOne))
      print ("List Two = ", listTwo)
      print ("List Two Sorted = ", bubbleSort (listTwo))

我认为您只需要一个方法,然后在两个列表中调用call it。您可以尝试以下方法: 这是一种为你做两件事的方法

listOne = [3, 9, 2, 6, 1]
listTwo = [4, 8, 5, 7, 0]

def bubblesort(array):
    for i in range(len(array)):
        for j in range(len(array) - 1):
            if array[j] > array[j + 1]:
                swap = array[j]
                array[j] = array[j + 1]
                array[j + 1] = swap
    print(array)


bubblesort(listOne)
bubblesort(listTwo)
[1,2,3,6,9]

[0,4,5,7,8]


你听说过一个叫做“方法”的范例吗?如果没有,您可以在这里阅读:对于python:谢谢!刚刚编写了另一个子程序来对第二个列表进行冒泡排序。@user662973我很乐意提供帮助