The paper contains 1657 words and is expected to last 3 minutes
Credit: Drew Beamer/Unsplash
Python can also be a lot of fun. It’s not as hard as you might think to revisit some of the built-in functions that aren’t as familiar at first, but why? Today, this article takes a closer look at three Python functions that are used more or less in everyday work, but are mostly ignored.
They may not save a lot of time (if you understand the logic behind them), but they make the code look cleaner. This may not sound like a big deal, but in the long run, it can be very rewarding for readers. Let’s start with the first function!
1. map()
Map () is a built-in Python function that applies a function to a sequence of elements (such as a list or dictionary). It is probably the simplest and most readable way to manipulate data.
The following example aims to square the numbers in a list. First, you must specify the function you are using. Next, I show and compare methods using map() and non-map (), that is, Python and non-Python.
nums = [1, 2, 3, 4, 5]#
this function will calculate square
def square_num(x):
return x**2
# non-pythonic approach
squares = []
for num in nums:
squares.append(square_num(num))
print(‘Non-Pythonic Approach: ‘, squares)
# pythonic approach
x = map(square_num, nums)
print(‘Pythonic Approach: ‘, list(x))
The output is essentially the same, but the Python approach is significantly cleaner and the process does not need to loop.
2. zip ()
Zip () is one of my favorite functions to use. It allows the user to iterate over two or more lists simultaneously. This feature is useful when dealing with both date and time issues.
For example, if used daily at work, the event starts when the user has the first attribute and ends when the user has the second attribute. Further thinking, there is always a need to calculate the time difference between events, and ZIP is by far the simplest implementation.
The example creates two lists containing numbers, and the task is to sum the corresponding elements:
first = [1, 3, 8, 4, 9]
second = [2, 2, 7, 5, 8]
# Iterate over two or more list at the same time
for x, y in zip(first, second):
print(x + y)
It’s simple and clean.
3. filter()
The filter() function is somewhat similar to the map() function — again, it applies a function to a sequence, except that filter() returns only elements with a value of True.
In the following example, I create a list of arbitrary numbers and a function that returns True if the number is even. Again, I’ll show you how to do this in non-Python and Python ways.
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Will return true if input number is even
def even(x):
return x % 2 == 0
# non-pythonic approach
even_nums = []
for num in numbers:
if even(num):
even_nums.append(num)
print(‘Non-Pythonic Approach: ‘, even_nums)
# pythonic approach
even_n = filter(even, numbers)
print(‘Pythonic Approach: ‘, list(even_n))
Similarly, the Python approach is cleaner and more readable — something that readers learn for a lifetime.
Recommended Reading topics
Leave a comment like follow
We share the dry goods of AI learning and development. Welcome to pay attention to the “core reading technology” of AI vertical we-media on the whole platform.
(Add wechat: DXSXBB, join readers’ circle and discuss the freshest artificial intelligence technology.)