Sorting of basic Python knowledge and sorting of basic python knowledge
In Python development, we often use sorting methods. The simplest method of sorting is to use the sort (list) function, it accepts a list and returns a new list with ordered elements. The original list is not changed.
A = [5, 1, 4, 3]
Print sorted (a) # [1, 3, 4, 5]
Print a ## [5, 1, 4, 3]
This is the most common way to pass a list to the sort () function, but it can actually be used as an input set of any type that can be iterated. The old list. sort () method is an alternative solution described below. The sort () function seems easier to use than sort (), so I suggest using sort ().
The sorting () function can be customized with optional parameters. Sort () optional parameter reverse = TRUE, such as sort (list, reverse = TRUE), so that its sorting regresses.
Strs = ['A', 'bb', 'z', 'cc']
Print sorted (strs) # ['bb', 'cc', 'AA', 'zz '] (case sensitive)
Print sorted (strs, reverse = True) # ['zz ', 'A', 'cc', 'bb']
Use buttons for custom sorting =
For more complex custom sorting, sorting () has an optional "Key =" specify "key" function, each element is compared before. The key function takes place at 1 and returns 1. The returned "proxy" value is used for comparison in sorting.
For example, you can use the string list to specify the string that is sorted by length by the key = LEN (built-in len () function), from short to long. The len () of the sort call gets a list of proxy length values for each string, and a variety of values associated with these proxies.
Strs = ['ccc ', 'aaa', 'D', 'bb']
Print sorted (strs, key = len) # ['D', 'bb', 'ccc ', 'aaa']
For another example, specifying "str. lower" as the main function is a forced sort that treats uppercase and lowercase letters as the same:
# "Key" argument specifying str. lower function to use for sorting
Print sorted (strs, key = str. lower) # ['A', 'bb', 'cc', 'zz ']
You can also transmit key functions in your MyFn, as shown in the following figure:
# Say we have a list of strings we want to sort by the last letter of the string.
Strs = ['xc ', 'zb', 'yd', 'wa ']
# Write a little function that takes a string, and returns its last letter.
# This will be the key function (takes in 1 value, returns 1 value ).
Def MyFn (s ):
Return s [-1]
# Now pass key = MyFn to sorted () to sort by the last letter:
Print sorted (strs, key = MyFn) # ['wa ', 'zb', 'xc', 'yd ']
To use key = custom sorting, remember that you provide a function that has a value and returns a proxy value to guide sorting. There is also an optional parameter "CMP = cmpFn" to sort (), specifying a traditional comparison function of two parameters, which is from the list of two values, return negative/0/positive indicating your order. In the string, the integer comparison function is built in,... is CMP (A, B), So you often need to call CMP () in your custom comparison. A newer parameter key = sorting is generally desirable.
Sort () method
As an alternative to sort (), the sort () method sorts the list and lists the ascending order, such as list. sort (). The sort () method changes the underlying list and returns None:
Alist. sort () # correct
Alist = blist. sort () # NO incorrect, sort () returns None
The above is a very common misunderstanding using sort ()-It * does not return * the sorting list. The sort () method must be called on the list; it does not work with any enumeration set (but is applicable to anything higher than the sort () function ). This sort () method sorts () functions early, so you may see it in the old code. The sorting () method does not need to create a new list, so you can sort these elements more quickly when the list is already in progress.
Tuples
A tuple is a component, such as a fixed-size group of (X, Y) coordinates. Tuples are like lists. Unless they are unchanged, they do not change the size (whether the tuples are strictly unchanged, because one of the elements involved may be variable ). Tuples serve as a "structure" in Python-a convenient way to bypass a small logic, fixed size value package. A function that needs to return multiple values can return only the tuples of the returned values. For example, if I want to have a three-dimensional coordinate list, the natural Python represents a list of tuples, where the size of each tuple is 3 holding one (X, Y, Z) group.
To create a tuples, only values separated by commas (,) are listed. Empty tuples are just a pair of empty parentheses. Accessing an element in a tuple is like a list-LEN () [], because it is waiting for all work.
Tuple = (1, 2, 'Hi ')
Print len (tuple) #3
Print tuple [2] # hi
Tuple [2] = 'bye' # NO, tuples cannot be changed
Tuple = (1, 2, 'bye') # this works
To create a single & #45; size, a single-byte element must be followed by a comma.
Tuple = ('hi',) # size-1 tuple
It is an interesting syntax case, but the comma is necessary to distinguish between tuples from the normal situation of the input parentheses expression. In some cases, you can omit the parentheses and Python will see from the comma of your intended tuples.
Specify the values of all corresponding values for the same size of a tuples with variable names. If the size of the tuples is different, it throws an error. This function is also available for the list.
(X, y, z) = (42, 13, "hike ")
Print z # hike
(Err_string, err_code) = Foo () # Foo () returns a length-2 tuple
List resolution (optional)
List connotation is a more advanced feature, which is good for some situations, but does not need to be practiced, rather than the first time you need to learn (that is, you can skip this section ). List Parsing is a compact method for writing expressions that extend to the entire list. Suppose we have a list NUMS [, 3]. Here is the list resolution to calculate the list of their squares [, 9]:
Nums = [1, 2, 3, 4]
Squares = [n * n for n in nums] # [1, 4, 9, 16]
Syntax [expr for var in list]-in for var in list looks like a common for loop, but there is no colon (:). The left side of the EXPR grants each element a new list of values for calculation once. The following is an example of a string in which each character string is changed to an uppercase letter '! "Appendix:
Strs = ['hello', 'and', 'Goodbye ']
Shouting = [s. upper () + '!!! 'For s in strs]
# ['Hello !!! ',' AND !!! ', 'Goodbye !!! ']
You can add, if the right of the for Loop is obtained, to narrow down the result. The if test evaluates each component, including only elements that are true in the test.
# Select values <= 2
Nums = [2, 8, 1, 6]
Small = [n for n in nums if n <= 2] # [2, 1]
# Select fruits containing 'A', change to upper case
Fruits = ['apple', 'cherry', 'bannana ', 'limon']
Afruits = [s. upper () for s in fruits if 'A' in s]
# ['Apple', 'bannana ']