Sorting Python 2-3 reverse with a list of base

introduction

The list is sorted together in a specific format, sometimes we do not like this sort, and we hope that it can be positive sequence or reverse order according to our way or the other sort

And reverse sequencing

For example, I have a list here, all values inside the store, but very messy, it will be you in ascending or descending order
You can sort the list by the positive sequence, it can also sort in reverse order, sort partition permanent of temporary and

Reverse list

Reverse list, use reverse()the method to sort a list element in turn
Note: is sort the list element in turn, not the reverse sort
reverse()method to sort the list of elements changes permanent, but can be recalled at any time reverse()to restore the original ordering

num = [ 23 , 12 , 53 , 6 , 18 , 68 , 55 , 96]
num.reverse()
print(num)  
# 运行结果:[96, 55, 68, 18, 6, 53, 12, 23]

From these examples, the use of reverse()methods to reverse the sort is only the beginning and end swap list elements reversed only, and no reverse order

List sorted

The list can be sorted numbers, letters

1. sorting positive sequence, using sort()methods of positive sequence sorting, the sorted restore the original non-permanent, the sort
cars = ['bmw', 'audi', 'toyota', 'subaru']
cars.sort()
print(cars)
# 运行结果:['audi', 'bmw', 'subaru', 'toyota']
2. reverse sort, in sort()the incoming parameters within parentheses method reverse=Trueto achieve the effect of reverse order. Permanent, after sorting no longer maintain the original sort of
cars = ['bmw', 'audi', 'toyota', 'subaru']
cars.sort( reverse=True )
print(cars)
# 运行结果:['toyota', 'subaru', 'bmw', 'audi']
3. Positive sequence sort (temporary), using sorted()the list temporary sort (positive sequence), does not affect the original list is sorted
num = [ 23 , 12 , 53 , 6 , 18 , 68 , 55 , 96]

print(num)
# 运行结果:[23, 12, 53, 6, 18, 68, 55, 96]

print( sorted(num))
# 运行结果:[6, 12, 18, 23, 53, 55, 68, 96]

print(num)
# 运行结果:[23, 12, 53, 6, 18, 68, 55, 96]

Print see from the above example, the use sorted()of a list of temporary positive sequence ordering again when printing the list, or the original sort order

4. reverse order (provisional), the sorted()method passing reverse=Trueachieve reverse order effect, does not affect the original list is sorted
num = [ 23 , 12 , 53 , 6 , 18 , 68 , 55 , 96]

print(num)
# 运行结果:[23, 12, 53, 6, 18, 68, 55, 96]

print( sorted(num,reverse=True))
# 运行结果:[96, 68, 55, 53, 23, 18, 12, 6]

print(num)
# 运行结果:[23, 12, 53, 6, 18, 68, 55, 96]

Print see from the above example, by sorted()inside pass reverse=Truewere temporarily reverse the sort parameter list
when the list is printed again, or the original sort order

other

Using the range()function to generate a series of digital

Only need to pass in parentheses start value and the end value on the line, this also follows the left and right opening and closing the principles
such as the following this example, print 0to 5digital, the actual print only 0to4

for num in range(0,5):
    print(num,end='\t')
# 运行结果:0    1   2   3   4

If you want to enter 0to 5the need to enter here

Create a list of numbers

Use list()function to range()generate a series of numbers into a list

list_num = list(range(0,5))
print(list_num)
# 运行结果:[0, 1, 2, 3, 4]

range()You may also be specified step length, in fact range()a function of three parameters can be passed, the start value, end, step

num_list = []
for num in range(0,20,2):
    num_list.append(num)
print(num_list)
# 运行结果:[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

Like the above example, the function range()from the 0beginning, continuously add 2 until it reaches or exceeds 20

Guess you like

Origin www.cnblogs.com/dazhi-blog/p/11440036.html