python small problem

[6,2,11,1,4,9,5,7,8,20] will put it all on the left than 6 small numbers, big into the right 6.

list1 = [6,2,11,1,4,9,5,7,8,20]

# method 1

To re-spliced ​​through three head operation, returns a new list

[i for i in list1 if i < 6] + [j for j in list1 if j >6]

 

Method # 2

Restructuring in the original list

= List1 [6,2,11,1,4,9,5,7,8,20]
for I in Range (len (List1)):
    IF List1 [I] <. 6:
        list1.insert (0, List1. pop (i)) # Although changes in the list, but the unprocessed data is not shifted, insert only deal with the front section, or may be unprocessed traversed

 

For I in Range >>> (len (List1)):
... Print (List1 [I])
... Print (List1)
... IF List1 [I] <. 6:
... list1.insert (0 , list1.pop (I))
...
. 6
[. 6, 2,. 11,. 1,. 4,. 9,. 5,. 7,. 8, 20 is]
2
[. 6, 2,. 11,. 1,. 4,. 9,. 5,. 7 , 8, 20]
11
[2, 6, 11, 1, 4, 9, 5, 7, 8, 20] 
1
[2, 6, 11, 1, 4, 9, 5, 7, 8, 20] 
4
[1, 2, 6, 11, 4, 9, 5, 7, 8, 20] For example, in the process # '1' time, while the seat 1 is changed, but not the position data of the latter are processed did not affect, in the traversal will not lack
9
[4, 1, 2, 6, 11, 9, 5, 7, 8, 20]
5
[4, 1, 2, 6, 11, 9, 5, 7, 8, 20]
7
[5, 4, 1, 2, 6, 11, 9, 7, 8, 20]
8
[5, 4, 1, 2, 6, 11, 9, 7, 8, 20]
20
[5, 4, 1, 2, 6, 11, 9, 7, 8, 20]

 

Guess you like

Origin www.cnblogs.com/su-sir/p/11795293.html