程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

How to use Python clip and range functions

編輯:Python

Python clip And range How to use function

This article introduces in detail “Python clip And range How to use function ”, Detailed content , The steps are clear , The details are handled properly , Hope this article “Python clip And range How to use function ” The article can help you solve your doubts , Let's follow Xiaobian's ideas and go deeper slowly , Let's learn new knowledge together .

One 、range Definition of function

range The() function generates a starting value of start, The final value does not exceed stop, In steps of step Equal difference sequence of .range The basic calling syntax of the function is as follows :

range(start, stop[, step])

start: The starting value of the array , Omission , The default value is 0.

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

step: step , Omission , The default value is 1, That is, the difference between the two numbers in the array .

Two 、range Function instance

Omit the starting value start And step length step

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

Get the results :

0
1
2
3
4
5

You can find range Function can omit the initial value start( The default value is 0) And step length step( The default value is 1), And take the default value to generate the arithmetic sequence .

Set initial value, final value and step size

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

Get the results :

5
7
9
11
13
15

You can find range The function generates an initial value of 5, The final value does not exceed 16( Maximize ), In steps of 2 Equal difference sequence of .

3、 ... and 、random.randint Definition of function

random.randint The function is numpy In the library , Usually you need to load first numpy library , Call this function again . The basic calling syntax of the function is as follows :

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

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

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

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

random.randint The integer array() function returns a random integer number, array, or data frame .

Range from low( contain ) To high( Not included ), namely [low, high). If no parameters are written high Value , Then the data range is [0, low).

Four 、random.randint Function instance

Random generation 5 individual [0, 6) Integer between

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

Get the results :

0
1
5
1
4

You can find random.randint If there is only one number in the function , Then a data range is generated [0, This number ) The integer of .

Random generation [-2, 9) Between 1 Dimension group

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

Get the results :

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

You can find random.randint Function size Values can control the dimension of data . The first number refers to the number of rows of data , The second number refers to the number of columns of data . example 2 Generate a 1 That's ok 6 Array of columns .

Random generation [5, 10) Between 3 That's ok 5 Column data box

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

Get the results :

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

You can find random.randint Function size Values can control the dimension of data . The first number refers to the number of rows of data , The second number refers to the number of columns of data . example 3 Generate a 3 That's ok 6 Column data box .

5、 ... and 、clip Definition of function

clip The function is numpy In the library , Usually you need to load first numpy library , Call this function again .clip The basic calling syntax of the function is as follows :

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

a: Array or data frame .

a_min: Lower bound , The minimum value of the interval ,a Middle ratio a_min Small numbers are forced to become a_min.

a_max: upper bound , The maximum value of the interval ,a Middle ratio a_max Large numbers are forced to become a_max.

out: You can specify the object of the output matrix ,shape And a identical .

The purpose of this function is to a All numbers in are limited to a_min and a_max In this interval , Values beyond this range are truncated and set to the limit value .

6、 ... and 、clip Function instance

Apply the values in the array clip Function

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

Get the results :

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

compare The previous is the original value , The next is to use clip Function intercepts the value after . You can find clip The < span class = span class = span class = span class = span class = span class = span class = span class = span class = span class = span class = span class = span class = span class = span class = span class = span class = span class = span class a_min And greater than a_max The values of are forced to be bound values .

Apply the values in the data frame clip Function

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

Get the results :

[[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]]

You can find clip The < span > function sets the data frame to less than a_min And greater than a_max The values of are forced to be bound values .

Read here , This article “Python clip And range How to use function ” The article has been introduced , If you want to master the knowledge points of this article, you need to practice and use it yourself to understand , If you want to know more about this article , Welcome to the Yisu cloud industry information channel .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved