Python learns the note taking _ data sorting method

  • 2020-04-02 13:37:50
  • OfStack

1. In situ sort: sort() method is used to sort the data in the specified order and replace the original data with the sorted data (the original order is lost), such as:


>>> data1=[4,2,6,432,78,43,22,896,42,677,12]
>>> data1.sort()
>>> data1       # The original order is replaced 
[2, 4, 6, 12, 22, 42, 43, 78, 432, 677, 896]

2. Replication sort: the built-in function sorted() is used to arrange the data in the specified order and return an ordered copy of the original data (the original order is kept), such as:


>>> data1=[4,2,6,432,78,43,22,896,42,677,12]
>>> data2=sorted(data1)
>>> data1     
[4, 2, 6, 432, 78, 43, 22, 896, 42, 677, 12]    # Original order reservation 
>>> data2    
[2, 4, 6, 12, 22, 42, 43, 78, 432, 677, 896]    # Sort copies 
>>>


Related articles: