[Python commonly used function collection] clip function, range function, etc.

You should have heard that the application of Python allows you to reduce the repetitive workload of a day to a few minutes or even less. Since then, free working hours and study more and more efficient working methods. Further improve work efficiency and make work more brilliant. This is not an advertisement, this is a real hammer picture.
  
This article will explore the collection of common Python functions with you, so that you can understand the principles of these functions in the shortest time. You can also use the fragmented time to consolidate these functions to make you more efficient in the process of processing.
  
insert image description here
  
  

First, the definition of the range function

  
The function of the range function is to generate an arithmetic sequence whose starting value is start, the final value does not exceed stop, and the step size is step. The basic calling syntax of the range function is as follows:

range(start, stop[, step])

start: The starting value of the array, which can be omitted, and the default value is 0.

stop: The upper limit of the array, generating an arithmetic sequence that does not exceed this value.

step: step size, can be omitted, the default value is 1, which is the difference between the two numbers in the array.
  
  

Two, range function example

  

Example 1: Omit the starting value start and the step size step

  

for i in range(6):
    print(i)

got the answer:

0
1
2
3
4
5

It can be found that the range function can omit the initial value start (the default value is 0) and the step size step (the default value is 1), and take the default value to generate an arithmetic sequence.

  

Example 2: Set initial value, final value and step size

  

for i in range(5, 16, 2):
    print(i)

got the answer:

5
7
9
11
13
15

It can be found that the range function generates an arithmetic sequence with an initial value of 5, a final value of no more than 16 (maximized), and a step size of 2.

  
  

3. Definition of random.randint function

  
The random.randint function is in the numpy library. Usually, the numpy library needs to be loaded before calling this function. The basic calling syntax of a function is as follows:

import numpy as np
np.random.randint(low, high=None, size=None, dtype=int)

low: The randomly generated number must be greater than or equal to this value.

high: The randomly generated number should be smaller than this value.

size: Controls the size of the random number. When omitted, a single integer is output by default.

The purpose of the random.randint function is to return a random integer or integer array or integer data frame.

The range is from low (inclusive) to high (exclusive), ie [low, high). If the value of parameter high is not written, the data range is [0, low).

  
  

Four, random.randint function instance

  

Example 1: Randomly generate 5 integers between [0, 6)

  

for i in range(5):
    print(np.random.randint(6))

got the answer:

0
1
5
1
4

It can be found that if there is only one number in the random.randint function, an integer whose data range is [0, the number) is generated.

  

Example 2: Randomly generate a 1-dimensional array between [-2, 9)

  

np.random.randint(-2, 9, (1,6))

got the answer:

array([[ 6,  0,  6, -1, -2,  2]])

It can be found that the size value in the random.randint function can control the dimension of the data. The first number refers to the number of rows of data, and the second number refers to the number of columns of data. Example 2 generates an array with 1 row and 6 columns.

  

Example 3: Randomly generate a data frame with 3 rows and 5 columns between [5, 10)

  

np.random.randint(5, 10, (3, 5))

got the answer:

array([[6, 8, 8, 5, 8],
       [6, 9, 9, 7, 9],
       [9, 7, 7, 7, 8]])

It can be found that the size value in the random.randint function can control the dimension of the data. The first number refers to the number of rows of data, and the second number refers to the number of columns of data. Example 3 generates a data frame with 3 rows and 6 columns.

  
  

Five, the definition of the clip function

  
The clip function is in the numpy library, and it is usually necessary to load the numpy library before calling this function. The basic calling syntax of the clip function is as follows:

import numpy as np
np.clip(a, a_min, a_max, out=None, **kwargs)

a: array or data frame.

a_min: The lower bound, the minimum value of the interval, and any number in a smaller than a_min will be forced to become a_min.

a_max: upper bound, the maximum value of the interval, any number in a greater than a_max will be forced to become a_max.

out: The object that can specify the output matrix, the shape is the same as a.

The function of this function is to limit all the numbers in a to the interval of a_min and a_max, and the values ​​beyond this interval are truncated and set to the limit value.

  
  

6. Example of clip function

  

Example 1: Apply the clip function to intercept the values ​​in the array

  

a = np.array(range(1, 10))
a_min = 3
a_max = 8
print(a)
print('======compare======')
print(np.clip(a, a_min, a_max))

got the answer:

[1 2 3 4 5 6 7 8 9]
======compare======
[3 3 3 4 5 6 7 8 8]

The value before compare is the original value, and the value after is intercepted by the clip function. It can be found that the clip function forces the values ​​smaller than a_min and larger than a_max in the array to become limit values.

  

Example 2: Apply the clip function to intercept the values ​​in the data frame

  

a = np.random.randint(20, 50, (4, 4))
a_min = 30
a_max = 40
print(a)
print('====compare====')
print(np.clip(a, a_min, a_max))

got the answer:

[[40 39 35 21]
 [29 44 36 46]
 [47 40 40 26]
 [24 24 26 44]]
====compare====
[[40 39 35 30]
 [30 40 36 40]
 [40 40 40 30]
 [30 30 30 40]]

It can be found that the clip function forces values ​​smaller than a_min and larger than a_max in the data frame to become boundary values.

So far, the commonly used function collection 1 in Python has been explained. Congratulations, you have a better understanding of Python. Interested friends can go to the "Ali Yiyang's Code" official account to read historical articles to see if there will be any surprises.

  
You may be interested in:
Draw Pikachu with Python
Draw a word cloud map
with Python Draw 520 eternal heart beats with Python Python face recognition - you are the only one
in my eyes With sound and text) Use the py2neo library in Python to operate neo4j and build a relationship map Python romantic confession source code collection (love, rose, photo wall, confession under the stars)



Long press (scan) to recognize the QR code above to learn more Python and modeling knowledge, making your study and work more brilliant.

Guess you like

Origin blog.csdn.net/qq_32532663/article/details/125247212