Python built-in functions used in data processing


There are many built-in functions in python, which are not as well-known as print, but they are extremely powerful. Using them well can greatly improve code efficiency.
This time, let’s sort out 8 useful python built-in functions.

1. set()

set()Functions come in handy when a list needs to be deduplicated .

obj = ['a','b','c','b','a']
print(set(obj))
# 输出:{'b', 'c', 'a'}

set([iterable])Used to create a collection whose elements are unordered and non-repeated.
After the collection object is created, the functions of union, intersection and difference can also be used.

A = set('hello')
B = set('world')

A.union(B) # 并集,输出:{'d', 'e', 'h', 'l', 'o', 'r', 'w'}
A.intersection(B) # 交集,输出:{'l', 'o'}
A.difference(B) # 差集,输出:{'d', 'r', 'w'}

2. eval()

eval(str_expression)role isconverts a string into an expression, and executes

a = eval('[1,2,3]')
print(type(a))
# 输出:<class 'list'>

b = eval('max([2,4,5])')
print(b)
# 输出:5

3. sorted()

In the process of processing data, we often use sorting operations, such as sorting the elements in lists, dictionaries, and tuples up/down.
That's where it comes in sorted() , it sorts any iterable and returns a list.
Sort the list in ascending order:

a = sorted([2,4,3,7,1,9])
print(a)
# 输出:[1, 2, 3, 4, 7, 9]

Operate in reverse order on tuples:

sorted((4,1,9,6),reverse=True)
print(a)
# 输出:[9, 6, 4, 1]

Use parameter: key, sort by string length according to custom rules:

chars = ['apple','watermelon','pear','banana']
a = sorted(chars,key=lambda x:len(x))
print(a)
# 输出:['pear', 'apple', 'banana', 'watermelon']

Sort a list of tuples according to custom rules:

tuple_list = [('A', 1,5), ('B', 3,2), ('C', 2,6)]
# key=lambda x: x[1]中可以任意选定x中可选的位置进行排序
a = sorted(tuple_list, key=lambda x: x[1])
print(a)
# 输出:[('A', 1, 5), ('C', 2, 6), ('B', 3, 2)]

4. reversed()

If you need to reverse the elements of a sequence, reversed()functions can help you.
reversed()Takes a sequence, reverses the elements in the sequence, and finally returns an iterator.

a = reversed('abcde')
print(list(a))
# 输出:['e', 'd', 'c', 'b', 'a']

b = reversed([2,3,4,5])
print(list(b))
# 输出:[5, 4, 3, 2]

5. map()

map() will map the specified sequence according to the provided function.

The first parameter function calls the function function with each element in the parameter sequence, and returns a new list containing each return value of the function function.

map() function syntax: map(function, iterable, ...)
parameters:

  • function – the function
  • iterable – one or more sequences
>>>def square(x) :            # 计算平方数
...     return x ** 2
... 
>>> map(square, [1,2,3,4,5])   # 计算列表各个元素的平方
[1, 4, 9, 16, 25]
>>> map(lambda x: x ** 2, [1, 2, 3, 4, 5])  # 使用 lambda 匿名函数
[1, 4, 9, 16, 25]
 
# 提供了两个列表,对相同位置的列表数据进行相加
>>> map(lambda x, y: x + y, [1, 3, 5, 7, 9], [2, 4, 6, 8, 10])
[3, 7, 11, 15, 19]

When doing text processing, if you want to convert each word in the sequence to uppercase.
This time you can use map()the function.

chars = ['apple','watermelon','pear','banana']
a = map(lambda x:x.upper(),chars)
print(list(a))
# 输出:['APPLE', 'WATERMELON', 'PEAR', 'BANANA']

map()According to the provided function, the specified sequence will be mapped, and finally an iterator will be returned.
That is to say, map()the function will process each element in the sequence with the specified method, and finally return the processed sequence to you.
For example, to square each number in a list:

nums = [1,2,3,4]
a = map(lambda x:x*x,nums)
print(list(a))
# 输出:[1, 4, 9, 16]

6. reduce()

As mentioned earlier, each number in the list is squared, using the map() function.
Then I want to multiply each element in the list, how to do it?
At this time, reduce()the function is used.

from functools import reduce
nums = [1,2,3,4]
a = reduce(lambda x,y:x*y,nums)
print(a)
# 输出:24

The first and second elements are first subjected to function operations, and the generated results are then subjected to function operations with the third element, and so on, to finally generate the result of the cumulative operation of all elements.

Another example, concatenating letters into a string:

from functools import reduce
chars = ['a','p','p','l','e']
a = reduce(lambda x,y:x+y,chars)
print(a)
# 输出:apple

You may have noticed that reduce()functions are no longer built-in functions in python3, but migrated to the functools module.
The function is mentioned here reduce()because it is too important.

7. filter()

What should I do if I want to remove even numbers from a list of numbers?

nums = [1,2,3,4,5,6]
a = filter(lambda x:x%2!=0,nums)
print(list(a))
# 输出:[1,3,5]

filter()The function easily completes the task. It is used to filter the sequence, filter out the elements that do not meet the conditions, and return an iterator object.
filter()Functions are similar to map()functions reduce(), which map each element in the sequence to a function and finally return the result.
Let's try again, how to pick out words that contain letters from many words w:

chars = chars = ['apple','watermelon','pear','banana']
a = filter(lambda x:'w' in x,chars)
print(list(a))
# 输出:['watermelon']

8. enumerate()

In such a scenario, each element in the sequence and its corresponding sequence number are printed at the same time. Let's use the enumerate() function to see.

chars = ['apple','watermelon','pear','banana']
for i,j in enumerate(chars):
    print(i,j)

'''
输出:
0 apple
1 watermelon
2 pear
3 banana
'''

enumerateThe translation means enumeration and enumeration, so enumerate()the function is used to perform operations on the elements in the sequence 顺序标注and return an iterator composed of (element, index).
As another example, mark the string and return each letter and its index:

a = enumerate('abcd')
print(list(a))
# 输出:[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd')]

Guess you like

Origin blog.csdn.net/Waldocsdn/article/details/105109196