Eight advanced functions commonly used in Python

Python crawler, data analysis, website development and other case tutorial videos can be viewed online for free



They are also called anonymous functions in other languages. If you don't want to use a function twice in your program, you may want to use lambda expressions, which are exactly the same as ordinary functions.

lambda argument: manipulate(argument)

lambda parameter: operation (parameter)

add = lambda x, y: x + y

print(add(3, 5))
# Output: 8
a = [(1, 2), (4, 1), (9, 10), (13, -3)]

def f(x):
    return x[1]

# a.sort(key=f)
a.sort(key=lambda x: x[1])

# Output: [(13, -3), (4, 1), (1, 2), (9, 10)]



sorted(iterable, *, key=None, reverse=False)


Returns a new sorted list from the item in iterable.

There are two optional parameters that must be specified as keyword parameters.

Key specifies a function with one parameter to extract the comparison key from each list element: key=str.lower. The default value is {None (compare elements directly).

Reverse is a Boolean value. If set to True, the list elements are sorted in the reverse order of each comparison.

The built-in sorting () function is stable. Sorting is stable if you ensure that the relative order of the elements that compare equally is not changed.

Ternary expression

Ternary operators are often called conditional expressions in Python. These expressions are based on true / false conditional judgment

It allows quick judgment with a simple line rather than complex multi line if statements. This is useful most of the time and makes the code simple and maintainable.

# If the condition is true, return true; otherwise, return false
condition_is_true if condition else condition_is_false
if condition:
    result = condition_is_true
    result = condition_is_false



map(function, iterable, ...)

Returns an iterator that applies function to each iterable item to produce a result. If additional iterable parameters are passed, function must take multiple parameters and apply to projects in all iterations in parallel. When using multiple iterators, the iterator stops when the shortest iterator runs out.

In [54]: list1 = [1, 2, 3, 4, 5, 6]

In [55]: list2 = [4, 3, 7, 1, 9]

In [56]: list(map(str, list1))
Out[56]: ['1', '2', '3', '4', '5', '6']

In [57]: list(map(lambda x, y: x+y, list1, list2))
Out[57]: [5, 5, 10, 5, 14]



enumerate( iterable, start=0)

Returns an enumerated object. iterable must be a sequence, an iterator, or other object that supports iteration. Of the iterator returned by {enumerate()__ next__ The () method returns a tuple containing a count (starting from start, the default value is 0) and the value obtained by traversing the iteration.

>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]




Make an iterator that aggregates elements from each iterator.

Returns an iterator of tuples, where the ith tuple contains the ith element from each parameter sequence or iteration. When the shortest input iteration is exhausted, the iterator stops. Using a single iteration parameter, it returns an iterator of 1 tuple. Without parameters, it returns an empty iterator.

Used with the * operator, * zip() can be used to extract the list:

>>> x = [1, 2, 3]
>>> y = [4, 5, 6]
>>> zipped = zip(x, y)
>>> list(zipped)
[(1, 4), (2, 5), (3, 6)]
>>> x2, y2 = zip(*zip(x, y))
>>> x == list(x2) and y == list(y2)
data = zip(list1, list2)
data = sorted(data)
list1, list2 = map(lambda t: list(t), zip(*data))



filter(function, iterable)

Construct an iterator with iterable elements whose functions return true. iterable can be a sequence, a container or iterator that supports iteration. If function is None, it is assumed that the identification function is false, that is, all elements that are false are deleted.

# Filter 0-10 Even number between
In [8]: list(filter(lambda x: x%2==0, range(10)))
Out[8]: [0, 2, 4, 6, 8]



The usage of the reduce function is very similar to that of map. It is also a function f and a list, but the entry parameters of the function must be two. Reduce also calls each element repeatedly and finally returns the final value, while map returns a list

Reduce in Python 3 has been removed from the global function. If necessary, use from functools import reduce

Posted by sssphp on Fri, 15 Apr 2022 19:48:51 +0930