sort and sorted

 

sort
list.sort(cmp=None, key=None, reverse=False)
sorted
aaa=sorted(iterable,cmp=None, key=None, reverse=False)
 
>> reverse when forward sort = False, when reverse = True to reverse the sort order. The default is False.
>> list.sort () method is only defined list, sorted () can be used to target any one iteration.
>> list.sort () will change the original list, if not the original list, this efficiency is slightly higher; sorted () does not change the original iterable, but returns a new sorted iterable.
 
Example:
a = [3,2,6,7,1]
a.sort()
print a 
b = [3,2,6,7,1]
print sorted(b,reverse=True)
print b
Output:
[1, 2, 3, 6, 7]
[7, 6, 3, 2, 1]
[3, 2, 6, 7, 1]
 
 
Write a few lines of code, the order-by-comparison and sequencing, and better understand the sort sorted sort of a convenience key.
a = [3,2,6,7,1]
for i in range(0, len(a)): 
    for j in range(0, i): 
       if a[i]<=a[j]: 
          k=a[i]
          a[i]=a[j]
          a[j]=k
           print a       
print a
Output:
[2, 3, 6, 7, 1]
[1, 3, 6, 7, 2]
[1, 2, 6, 7, 3]
[1, 2, 3, 7, 6]
[1, 2, 3, 6, 7]
[1, 2, 3, 6, 7]
 
 

Guess you like

Origin www.cnblogs.com/myshuzhimei/p/11751336.html