Life is short, learn Python quickly
Yes, you read that right, it is "... "
In Python... represents an object named Ellipsis. According to the official description, it is a special value that can usually be used as a placeholder for an empty function or used for slicing operations in Numpy.
For example:
def my_awesome_function(): ...
is equivalent to:
def my_awesome_function(): Ellipsis
Of course, you can also use pass or string as a placeholder:
def my_awesome_function(): pass
def my_awesome_function(): "An empty, but also awesome function"
Their final The effects are the same.
Next let’s talk about... how objects work in Numpy. Create a 3x3x3 matrix array, and then get the second column of all innermost matrices:
>>> import numpy as np >>> array = np.arange(27).reshape(3, 3, 3) >>> array array([[[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8]], [[ 9, 10, 11], [12, 13, 14], [15, 16, 17]], [[18, 19, 20], [21, 22, 23], [24, 25, 26]]])
In order to get the second column of the top-level matrix, the traditional method may be like this:
>>> array[:, :, 1] array([[ 1, 4, 7], [10, 13, 16], [19, 22, 25]])
If you can use... object, it is like this:
>>> array[..., 1] array([[ 1, 4, 7], [10, 13, 16], [19, 22, 25]])
But please note that . .. objects only work with Numpy, not Python built-in arrays.
Decompression of iteration objects is a very convenient feature:
>>> a, *b, c = range(1, 11) >>> a 1 >>> c 10 >>> b [2, 3, 4, 5, 6, 7, 8, 9]
or:
>>> a, b, c = range(3) >>> a 0 >>> b 1 >>> c 2
Similarly, instead of writing like this Code:
>>> lst = [1] >>> a = lst[0] >>> a 1 >>> (a, ) = lst >>> a 1
You might as well perform a more elegant assignment operation like decompressing the iteration object:
>>> lst = [1] >>> [a] = lst >>> a 1
Although this seems a bit stupid, in my personal opinion, it is worse than the previous one The writing is more elegant.
There are various strange ways to expand arrays, for example:
>>> l = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] >>> flattened = [elem for sublist in l for elem in sublist] >>> flattened [1, 2, 3, 4, 5, 6, 7, 8, 9]
If you have a certain understanding of reduce and lambda, it is recommended to use more elegant Method:
>>> from functools import reduce >>> reduce(lambda x,y: x+y, l) [1, 2, 3, 4, 5, 6, 7, 8, 9]
The combination of reduce and lambda can perform splicing operations on each sub-array in the l array.
Of course, there is a more magical way:
>>> sum(l, []) [1, 2, 3, 4, 5, 6, 7, 8, 9] >>> # 其实相当于 [] + [1, 2, 3] + [4, 5, 6] + [7, 8, 9]
Yes, by performing sum operation on the two-dimensional array, you can "add" each element in the two-dimensional array. Piece it together.
In the same way, if you perform a sum operation on a three-digit array, it can be transformed into a two-dimensional array. At this time, if you perform a sum operation on the two-dimensional array, it can be expanded into a one-dimensional array.
Although this technique is excellent, I don’t recommend it because the readability is too poor.
Whenever you run an expression in the Python interpreter, IPython, or Django Console, Python will bind the output value to the _ variable:
>>> nums = [1, 3, 7] >>> sum(nums) 11 >>> _ 11 >>>
Since it is a variable, you can overwrite it at any time, or operate it like a normal variable:
>>> 9 + _ 20 >>> a = _ >>> a 20
Many people don’t know , else can be used in many places. In addition to the typical if else, we can also use it in loops and exception handling.
If you need to determine whether a certain logic is processed in the loop, this is usually done:
found = False a = 0 while a < 10: if a == 12: found = True a += 1 if not found: print("a was never found")
If else is introduced, we can use one less variable:
a = 0 while a < 10: if a == 12: break a += 1 else: print("a was never found")
We can use else in try...except... to write the logic when the exception is not caught:
In [13]: try: ...: {}['lala'] ...: except KeyError: ...: print("Key is missing") ...: else: ...: print("Else here") ...: Key is missing
In this way, if the program does not Exception, the else branch will be taken:
In [14]: try: ...: {'lala': 'bla'}['lala'] ...: except KeyError: ...: print("Key is missing") ...: else: ...: print("Else here") ...: Else here
If you often do exception handling, you will know that this technique is quite convenient.
The above is the detailed content of Five hidden tricks in Python you've probably never heard of. For more information, please follow other related articles on the PHP Chinese website!