Python built-in functions summary

  1. lambda - anonymous function

python lambda functions are anonymous functions, meaning it does not require an anonymous function is the function name, because some functions are too simple, does not need to take a function name alone.

# 将一个数据添加100
def add(value):
    return value + 100

# 等价于:
f = lambda x: x + 100

  1. enumerate - enumeration function

For (Iterable) objects (such as a list, string) an iteration / traversable, which is composed of the enumerate a sequence index, which can be obtained by using the index value and at the same time, the enumerate been used for counting in a for loop.

# 获取数据及下标
lst = ['This','is','a','test.']
print(list(enumerate(lst)))
[(0, 'This'), (1, 'is'), (2, 'a'), (3, 'test.')]

# Stupid way 
lst = ['This','is','a','test.']
for i in range(len(lst)):
    print(i,lst[i])

# Good way
for index, item in enumerate(lst):
    print(index,item)

  1. [For ... in ... if] list expression

# =map
lst = [1,3,5,76,0,9]
result = [x+3 for x in lst]
print(result)
[4, 6, 8, 79, 3, 12]

# =filter
lst = [1,3,5,76,0,9]
result = [ x*2+8 for x in lst if x >5 ]
print(result)
[160, 26]

列表表达式比内置的map/filter/reduce更加清晰;
map/filter/reduce需要额外的lambda表达式的支持;
字典和集合也支持列表表达式;
  1. (For ... in ... if) generator expression

The lack of a list comprehension is necessary to generate all of the data to create the entire list. This may have a lot of data iterators have a negative effect. Generator expressions solves this problem by combining list comprehensions and generators.

items = ( x + 10 for x in range(10) if x % 2)
print(items)  # <generator object <genexpr> at 0x10ce3d390>
for i in items:
    print(i)

11
13
15
17
19
  1. map mapping function,

You need to perform operations on the list of fun every element;

# 语法: map(fun, iterable) --> map object

tp = (1,3,45,6)
result = map(lambda x: x +10 , tp)
print(result)  # <map object at 0x10dae90b8>

# 注意如下两种情况输出的结果不一样;
# 生产器生成后输出后就没有了
# eg1
print(list(result))
for i in result:
    print(i)
    
#--> [11, 13, 55, 16]

# eg2
for i in result:
    print(i)
print(list(result))
# -->11
# -->13
# -->55
# -->16

# 优雅的表达
print(tuple(map(lambda x: x+10,tp))) #(11, 13, 55, 16, 21)
# 优雅的表达
print(list(map(lambda x: x+10,tp)))  #[11, 13, 55, 16, 21]

# 注意事项
1. 列表中的元素类型,需要在函数中适配;
  1. filter

# 语法 filter(fun, iterable) ---> 可以迭代的序列, 相对fun的执行的结果是True
lst = [11, 13, 55, 16, 21]
result = filter(lambda x : x > 20, lst)
print(result)       # <filter object at 0x10db06d68>
print(list(result)) # [55, 21]
  1. reduce

# 语法: reduce(fun,iterable) --> function—result
# fun 连续作用iterable的每一个元素,新的参数为上一次执行的结果,

from functools import reduce # 在Python 3中,它被移动到functools模块,要使用前我们需要导入
lst =[11, 13, 55, 16, 21]
result = reduce(lambda x,y: x*y, lst)
print(result) #2642640

  1. zip

zip () function can be used for iterative object as a parameter, the corresponding element of the object packed into a tuple, then the object is returned from the group consisting of these elements, the benefits of doing so is to save a lot of memory.

# zip()是Python的一个内建函数,它接受一系列可迭代的对象作为参数,将对象中对应的元素打包成一个个tuple(元组),
# 然后返回由这些tuples组成的list(列表)。
# 若传入参数的长度不等,则返回list的长度和参数中长度最短的对象相同。利用*号操作符,可以将list unzip(解压) 
a = [1,2,3]
b = [4,5,6]
c = [7,8,9,10,11]
z1 = zip(a,b)
print(list(z1))  #[(1, 4), (2, 5), (3, 6)] 
z2 = zip(a,c)
print(list(z2))  # [(1, 7), (2, 8), (3, 9)]
z3 = zip(*z2)
print(list(z3))  # [(1, 2, 3), (7, 8, 9)]
  1. eval

# 表达式 eval(expression, globals=None, locals=None)
# expression:是一个字符串,python会使用globals字典和locals字典作为全局和局部的命名空间,将expression当做一个python表达式进行解析和计算。
a,b,c = 1,2,3
print(eval('a+b+c')) # 6

dict1 = {"d":4,"f":5,"e":18}
print(eval('d+f+e',dict1)) # 27

Guess you like

Origin blog.csdn.net/hcj1101292065/article/details/94758415