简体   繁体   English

在python中打印排序和未排序的列表

[英]Printing sorted and unsorted list in python

I have a code which generates a list of numbers and then sorts it. 我有一个代码,该代码生成一个数字列表,然后对其进行排序。 The sorting function works but I need to print the unsorted list after sorting. 排序功能有效,但我需要在排序后打印未排序的列表。 That's why I put the print function to print the unsorted but instead it prints the sorted list. 这就是为什么我使用print函数来打印未排序的内容,而是打印已排序的列表的原因。 I thought I stored the unsorted list by assigning the unsorted list to a variable and then try to print it in the end. 我以为我通过将未排序列表分配给变量来存储未排序列表,然后尝试最后打印出来。 Any ideas why it prints the sorted list instead of the unsorted? 有什么想法为什么要打印排序列表而不是未排序列表? I didn't put the GenerateNumbers() function as what is does is give the "numbers" variable a list of unsorted numbers.I'm using insertion sort. 我没有使用GenerateNumbers()函数,因为它给“ numbers”变量提供了一个未排序数字的列表。我正在使用插入排序。

def InsertionSort(sort_list):
    print("Sorting numbers...")
    for i in range(0, len(sort_list)-1):
       for j in range(i+1,0,-1):
         if(sort_list[j] < sort_list[j-1]):
             temp = sort_list[j-1]
             sort_list[j-1] = sort_list[j]
             sort_list[j] = temp
             print(sort_list)
          else:
             break
    print("Finished sorting.")
    print()
    return sort_list

numbers = GenerateNumbers()
unsorted = numbers
sort_list = numbers

sorted_numbers = InsertionSort(sort_list)
print("unsorted list:", numbers)
print("sorted list:", sorted_numbers)

You have three assignment commands 您有三个分配命令

numbers = GenerateNumbers()
unsorted = numbers
sort_list = numbers

You think that you have three different copies of the list, but you do not. 您认为您有该列表的三个不同副本,但是您没有。 Since a list is a mutable type, Python has made three different names for the same list. 由于列表是可变类型,Python为同一列表使用了三个不同的名称 Hence your confusion. 因此,您感到困惑。 Changing the contents one of those names changes them all immediately. 更改内容之一即会立即更改所有名称。 You need to make copies of the list. 您需要复制列表。 There are several ways to do this. 有几种方法可以做到这一点。 The two easiest ways are 两种最简单的方法是

numbers = GenerateNumbers()
unsorted = numbers[:]
sort_list = numbers[:]

and

numbers = GenerateNumbers()
unsorted = list(numbers)
sort_list = list(numbers)

There are other ways, such as deepcopy() , which are useful for more complicated data structures, but one of these two will suffice for this situation. 还有其他方法,例如deepcopy() ,对于更复杂的数据结构很有用,但是这两种方法中的一种就足够了。

When you're calling 打电话时

unsorted = numbers
sort_list = numbers

you're not actually copying the list, you're just copying the reference to the list. 您实际上并没有复制列表,只是将引用复制到列表。 unsorder and sort_list both point to the same list in the memory. unsordersort_list都指向内存中的同一列表。

To copy the list instead you can do: 要复制列表,您可以执行以下操作:

sort_list = list(numbers)

Now sort_list points to a new list in the memory and you can change sort_list without changing numbers . 现在sort_list指向内存中的新列表,您可以更改sort_list而不更改numbers

Your function sorts the list in place and hence sorted_numbers is sort_list , further, the assignment sort_list = numbers is simply a renaming, it does not create a new list and hence sort_list will be "the same" as "unsorted". 您的函数对列表进行了排序,因此sorted_numbers is sort_list ,此外,分配sort_list = numbers只是重命名,它不会创建新列表,因此sort_list将与“ unsorted”“相同”。 Thus, when you sort sort_list , unsorted will also become sorted. 因此,当您对sort_list unsorted时, unsorted也将变为已排序。

You can solve this by calling list on the numbers, which will cause python to create a copy. 您可以通过在数字上调用list来解决此问题,这将导致python创建一个副本。

numbers = GenerateNumbers()
unsorted = list(numbers)  # creates a copy
sort_list = numbers

sorted_numbers = InsertionSort(sort_list)
print("unsorted list:", numbers)
print("sorted list:", sorted_numbers)

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

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