python basis (11): Built-in functions and higher-order functions

Built-in functions: 

Lamba function can be used to sort the dictionary. (No built-in dictionary function, not () Sort by dict.sort)

dic = {'b':5, 'a':3, 'c':4}

# 直接用python函数sorted:
print(sorted(dic))
# output:['a', 'b', 'c']

print(sorted(dic.items()))                                        # 对key排序:
# output:[('a', 3), ('b', 5), ('c', 4)]
print(sorted(dic.items(), key = lambda x:x[1], reverse=True))     # 用lambda函数指定排序元素
# output:[('b', 5), ('c', 4), ('a', 3)]

print(sorted(dic.values()))                                       # 只对值排序
# output:[3, 4, 5]

print(sorted(dic.values(), reverse = True))                       # 只对值逆序
# output:[5, 4, 3]

List element when the dictionary, you can specify the dictionary elements to sort.

list1 = [
        {'name':'joe', 'age':'18'},
        {'name':'susan', 'age':'19'},
        {'name':'tom', 'age':'17'}
        ]

print(sorted(list1, key = lambda x:x['name']))
# output:[{'name': 'joe', 'age': '18'}, {'name': 'susan', 'age': '19'}, {'name': 'tom', 'age': '17'}]
print(sorted(list1, key = lambda x:x['age']))
# output:[{'name': 'tom', 'age': '17'}, {'name': 'joe', 'age': '18'}, {'name': 'susan', 'age': '19'}]

Common built-in functions:

method description example
abs() Returns the absolute value function

a = -1

print (abs (num))

# output:1

sorted(list) Sort, after the return sorted list

print(sorted(['a','b','','d']))

# output:['', 'a', 'b', 'd']

sum(list) Seeking list of elements and

sum([1,2,3])

# output:6

round(a, b) Gets the specified number of digits of trees. is a floating-point number, b is the number of bits to be retained

round(3.1415926)

# output:3.14

isinstance(a, b) Type judgment, a is a variable to be determined, b is the type

a = 1

print(isinstance(num,int))

# output:True

eval() Performing an expression, as an arithmetic or string

eval('1+1')

# output:2

exec() Output python statement

exec('print("Python")')

# output:Python

Built-in functions can be queried by dir (__ builtins__). 

Higher-order functions:

method description

map(func, seq[,seq[,seq...]])

- > list

 Receiving a set of functions and a plurality of sequences, it will be provided as a function of mapping the specified sequence, and then returns a new map object.

filter(func, seq)

- >  list or tuple or string

Sequences for filtering, the filter element does not conform to the conditions, the object returned by the qualified filter elements.
reduce(func, seq[, initvalue]) For all elements of a sequence of data calls func merge operation may be given an initial value.

map:

Example 1:

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

# map
new_list = map(lambda x:x*2, list1)
print(new_list)
# output:<map object at 0x0000013AB0630128>

print(list(new_list))  # 将map对象转换为list
# output:[2, 4, 6, 8, 10]

# 或者
print([x*2 for x in list1])
# output:[2, 4, 6, 8, 10]

Example 2:

list1 = [1,3,5,7,9]
list2 = [2,4,6,8,10]

# map
new_list = map(lambda x, y : x*y, list1, list2)
print(new_list)
# output:<map object at 0x0000013AB0630128>

print(list(new_list))  # 将map对象转换为list
# output:[2, 12, 30, 56, 90]
# 即对应元素相乘

# 或者
print([x*y for x,y in zip(list1, list2)])
# output:[2, 12, 30, 56, 90]

filter:

Example 1:

list1 = [1,3,5,7,9]

# filter
new_list = filter(lambda x : x>4, list1)
print(new_list)
# output:<filter object at 0x0000013AB0692470>

print(list(new_list))  # 将map对象转换为list
# output:[5, 7, 9]

# 或者
print([x for x in list1 if x > 4])
# output:[5, 7, 9]

The above filter into a map:
 

new_list = map(lambda x : x>4, list1)
print(list(new_list))  # 将map对象转换为list
# output:[False, False, True, True, True]

All, map indicate map, filter represents the screening, the two are not interchangeable.

 

reduce:

Example:

from functools import reduce

list2 = [2,4,6,8,10]
reduce(lambda x,y:x+y, list2)
# output:30
# x+y: 2+4, 返回6
# x+y: 6+6, 返回12
# x+y: 12+8,返回20
# x+y: 20+20,返回30

reduce(lambda x,y:x+y, list2, 5)
# output:35
# x+y: 5+2, 返回7
# x+y: 7+4, 返回11
# ……

 

Guess you like

Origin blog.csdn.net/qq_26271435/article/details/89714035