python learning progress 12 (filter and solter)

Python built-in filter()functions for filtering sequences.

And map()the like, filter()also receives a function and a sequence. And map()the difference is, filter()the function passed successively to each element, based on the returned value Trueis Falsedecided to keep or discard the element.

For example, in a list in the even deleted, leaving only the odd number, you can write:

def is_odd(n):
    return n % 2 == 1 list(filter(is_odd, [1, 2, 4, 5, 6, 9, 10, 15])) # 结果: [1, 5, 9, 15] 

The empty string a sequence deleted, you can write:

def not_empty(s):
    return s and s.strip() list(filter(not_empty, ['A', '', 'B', None, 'C', ' '])) # 结果: ['A', 'B', 'C'] 

Visible with filter()this higher-order functions, the key is to correctly implement a "filter" function.

It noted that the filter()function returns a Iterator, which is an inert sequence, so to force filter()the completion of the calculation result, need list()access to all functions and return the results list.

 

 

Sorting algorithm is also frequently used in the program. Whether using bubble sort or quick sort, sort of the core is comparing two elements. If it is digital, we can directly compare, but if it is a string or two dict it? Direct comparison of the size of the mathematics makes no sense, therefore, the process must be abstracted by comparison function.

Python built-in sorted()function you can sort the list:

>>> sorted([36, 5, -12, 9, -21])
[-21, -12, 5, 9, 36]

In addition, sorted()the function is a higher order function, it may receive a keyfunction to implement custom sorting, such as sorting by the absolute value of:

>>> sorted([36, 5, -12, 9, -21], key=abs) [5, 9, -12, -21, 36] 

The specified function key acting on each element of the list, sorted by the result returned by the function key. The original list through comparison and key=abstreated list:

list = [36, 5, -12, 9, -21] keys = [36, 5, 12, 9, 21] 

Then sorted()function sorted by keys, and returns the corresponding list of elements in a corresponding relationship:

keys排序结果 => [5, 9,  12,  21, 36]
                |  |    |    |   |
最终结果     => [5, 9, -12, -21, 36]

We look at a string sorting example:

>>> sorted(['bob', 'about', 'Zoo', 'Credit'])
['Credit', 'Zoo', 'about', 'bob']

By default, sort strings, in accordance with the size of the ASCII comparison, since 'Z' < 'a'a result, capital letters Zwill be ranked in lowercase letters aforegoing.

Now, we propose to sort should ignore case, sorted in alphabetical order. To implement this algorithm, we do not have to change existing code big plus, as long as we can use a function key is mapped to a string sort can ignore case. To compare two strings ignoring case, in fact, put strings are capitalized (or all lowercase), then compare.

In this way, we have to sortedpass key function, you can achieve case-insensitive sort:

>>> sorted(['bob', 'about', 'Zoo', 'Credit'], key=str.lower) ['about', 'bob', 'Credit', 'Zoo'] 

To reverse the sort order, without changing the function key, the third parameter can be passed reverse=True:

>>> sorted(['bob', 'about', 'Zoo', 'Credit'], key=str.lower, reverse=True) ['Zoo', 'Credit', 'bob', 'about']

 

Guess you like

Origin www.cnblogs.com/2205254761qq/p/12310218.html