Commonly used built-in functions in python

Keywords: Python Lambda

Basic small function, string function, sequence function

Sequence, tuple, list small function

max() for maximum (list, tuple, sequence)
min() for minimum
len() for length

>>> a = [1,2,3,4]
>>> max(a)
4
>>> min(a)
1
>>> len(a)
4
>>>

Operation small function
divmod() returns a tuple. The first parameter is quotient and the second is remainder
pow(x,y) exponential operation, y power of X
The power of pow(x,y,z) x to the power of Y
round() floating point

>>> a = 3
>>> b = 4
>>> divmod(a,b)
(0, 3)
>>> divmod(b,a)
(1, 1)
>>> pow(a,b)
81
>>> pow(a,b,8)
1
>>>
>>> a/b
0.75
>>> round(a/b)
1
>>> round(a/b,2)
0.75
>>> round(a/b,4)
0.75
>>>

Other small functions
callable() tests whether a function can be called
isinstance(l,list) tests whether l is a list

>>> def f(x):
	pass

>>> callable(fc)
Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    callable(fc)
NameError: name 'fc' is not defined
>>> callable(f)
True
>>>
>>> l = [1,2,3,4]
>>> t = (2,3,4,5)
>>> s = 'hello'
>>> isinstance(l,list)
True
>>> isinstance(t,tuple)
True
>>> isinstance(s,str)
True
>>> isinstance(l,str)
False

String function

str.capitalize() initial
str.replace('x','y',count) string replace count several times
str.split("", sep) converts a string to a list, cuts it with "", and cuts it with sep several times

>>> str1 = 'hello world , today is very good day'
>>> str1.capitalize()
'Hello world , today is very good day'
>>> str1
'hello world , today is very good day'
>>> str1.replace('o','9',1)
'hell9 world , today is very good day'
>>> str1.replace('o','9',3)
'hell9 w9rld , t9day is very good day'
>>> str1.replace('o','9')
'hell9 w9rld , t9day is very g99d day'
>>>
>>> ip = '192.168.1.254'
>>> ip.split(".")
['192', '168', '1', '254']
>>> ip.split(".",1)
['192', '168.1.254']
>>>

Sequence function

filter() filter function
filter(f,l) passes the value in the list of L to function f for judgment, and keeps the required numeric function return True

zip() corresponds the values of the two lists and stores them in the list as tuples. Take the shortest as the combination number

map(None,a,b) corresponds the values of list a and B and passes them to the None function, which can be used as a function

fc(x,y)
reduce(fc,list) transfers the values of list to function fc in turn

>>> def f(x):
	if x>5:
		return True


>>> l = [1,2,3,5,6,2,3,6,7,8]
>>> filter(f,l)
<filter object at 0x00000220AC4C95E0>
>>> list(filter(f,l))
[6, 6, 7, 8]
>>>
>>> name = ['zhang','li','wang','zhou']
>>> age = [22,21,23,24]
>>> list(zip(name,age))
[('zhang', 22), ('li', 21), ('wang', 23), ('zhou', 24)]
>>> city = ['beijing','shanxi','xinjiang']
>>> list(zip(name,age,city))
[('zhang', 22, 'beijing'), ('li', 21, 'shanxi'), ('wang', 23, 'xinjiang')]
>>>
>>> def f(name,age):
	return name,age

>>> list(map(f,name,age))
[('zhang', 22), ('li', 21), ('wang', 23), ('zhou', 24)]
>>>
>>> def f(x,y):
	return x+y

>>> a = [1,2,3,4]
>>> b = [1,2,3,4]
>>> list(map(f,a,b))
[2, 4, 6, 8]
>>>
>>>
>>> l = range(100)
>>> reduce(f,l)
Traceback (most recent call last):
  File "<pyshell#29>", line 1, in <module>
    reduce(f,l)
NameError: name 'reduce' is not defined
>>>
>>> from functools import reduce
>>> reduce(f,l)
4950
>>> l = range(101)
>>> reduce(f,l)
5050
>>>
>>>

When using reduce, you need to import the corresponding modules.

Reduce is very convenient to calculate factorials. According to reduce, it can be written as a line of code.

>>> n = 101
>>> range(n)
range(0, 101)
>>> reduce(lambda x,y:x+y , l)
5050
>>>

+If I change it to *, I will find the factorial of n. Not the factorial of n-1.

Let's write a small example. It's more impressive.


There is always one on the way to study and keep fit

Posted by mnuzum on Sun, 26 Apr 2020 19:54:55 -0700