Generating a ternary expressions with expressions anonymous function built-in functions

A triplet of expressions

if ... else .. it can be turned into a branch line

grammar:

Condition holds return value if left to determine the conditions else condition does not hold the right of return value

#if 判断条件
#   执行
#else:
#   执行
#if判断语句
numb1 = 2
numb2 = 3
if numb1 > numb2:
    print(numb1)
else:
    print(numb2)
 #三元表达式
numb1 = 1
numb2 = 2
numb1 if numb1 >numb2 else numb2

List of Formula

You can generate a list of his party achieved

grammar:

list = [a value taken for each, for each arbitrary value of an object may be taken out in an iterative iterables]

It is right for the number of cycles, and the value may be taken every iteration object

for the left can add value to the current list

list = [a value for each iteration may be withdrawn in the object iterable]

#将list1中的值,依次取出,添加到new_list中
list1 = [1, 2, 3, 4]
new_list = []
for i in list1:
    new_list.append(i)
    
print(new_list)

Generator expressions

Generator is formula

--- list expression: the amount of data using hours

​ [list for line in range(1,6)] -----> [1, 2, 3, 4, 5 ]

优点:  可以依赖索引取值,取值方便

Cons: waste of resources

--- generator expression: when using large volumes of data

() ----> Returns generator

(Line for line in range (1,6)) ---> g ​​generators (1, 2, 3, 4, 5)

Advantages: save resources

Cons: The value is not convenient

#生成一个有1000个值的生成器
g = (line for line in range(1,1001))
print(g)
#列表生成式实现
list1 = [line for line in range(1,1001)]
print(list1)

Anonymous function

Function without a name

: Left parameter: the right is the return value

lambda :

Note: Because no name

Anonymous function requires a one-time use

Anonymous function alone is meaningless, need to be used in conjunction with the built-in function makes sense

Built-in functions

Built-in functions: range (), print (), len ()

Built-in method of providing internal python:

max、 min、sorted、map、 filter

selecting the maximum value for the minimum min max

list1 = [1, 2, 3, 4, 5]

max in the interior will list1 by extraction for each value, and determines

print(max(list1))

dict1 = {
    'tank': 1000,
    'egon': 500,
    'sean': 200,
    'jason': 50
}
print(min(dict1, key=lambda x : dict1[x]))
# sorted 排序 默认升序    reverse: 反转  reverse 默认 False
"""
map : 映射
    map(函数地址,可迭代对象)  ---> map 对象 
map 会将可迭代对象中的每一个值进行修改,然后映射一个 map对象
可以再将 map 对象转换成列表/元祖
注意:只能转换一次 

reduce : 合并
    reduce(函数地址,可迭代对象,初始值)
    reduce中,初始值默认为0 

filter: 过滤
    filter(函数地址,可迭代对象)
"""

map mapping (use)

name_list = ['egon', 'jason', 'sean', 'tank']
map_obj = map(lambda name: name + '喜欢吃生蚝' if name == 'tank' else name + 'dsb', name_list)
print(map_obj)
print(list(map_obj))
打印结果:
<map object at 0x00000182D780E488>
['egondsb', 'jasondsb', 'seandsb', 'tank喜欢吃生蚝']

reduce consolidated (usage)

#注意,reduce 无法单独使用 需要先调用
from functools import reduce 
每次从可迭代对象中获取两个值进行合并
reduce(lambda x, y : x + y, rangr(1, 101), 0)
需求:
    求1--100的和
普通:
init = 0
for i in rang(1,100):
    init += i
print(init)


reduce :
from functools import reduce
res = reduce(lambda x, y : x + y, range(1,101), 0)
print(res)

filter filtration (usage)

name_list = ['egon_dsb', 'sean_dsb', 'jason_dsb', 'tank']

filter_obj = filter(lambda name: name.endswith('_dsb'), name_list)

print(filter_obj)
print(list(filter_obj))

Guess you like

Origin www.cnblogs.com/127-2933/p/11867963.html