新聞中心
創(chuàng)新互聯(lián)Python教程:

創(chuàng)新互聯(lián)建站專注于巨鹿企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè)公司,商城網(wǎng)站建設(shè)。巨鹿網(wǎng)站建設(shè)公司,為巨鹿等地區(qū)提供建站服務(wù)。全流程定制制作,專業(yè)設(shè)計,全程項目跟蹤,創(chuàng)新互聯(lián)建站專業(yè)和態(tài)度為您提供的服務(wù)
編寫一個 Python 程序,將 Numpy 數(shù)組向左旋轉(zhuǎn) n 次或N個位置。為了向左旋轉(zhuǎn)數(shù)組,我們根據(jù)位置對數(shù)組進(jìn)行了切片,并將其與 Python 示例中的 numpy concatenate 函數(shù)相結(jié)合。
import numpy as np
ltArray = np.array([4, 7, 9, 11, 22, 55, 99, 125, 78, 345])
ltRotate = int(input("Enter Position to Left Rotate Array Elements = "))
print('Original Array Elements Before Left Rotating')
print(ltArray)
arr = np.concatenate((ltArray[ltRotate:], ltArray[:ltRotate]), axis = 0)
print('\nFinal Array Elements After Left Rotating')
print(arr)
這個 Python 程序使用 for 循環(huán)根據(jù)用戶給定的位置向左旋轉(zhuǎn) Numpy 數(shù)組項。
import numpy as np
ltArray = np.array([19, 25, 32, 77, 28, 99, 44, 88])
ltRotate = int(input("Enter Position to Left Rotate Array Elements = "))
print('Original Array Elements Before Left Rotating')
print(ltArray)
for i in range(ltRotate):
firstValue = ltArray[0]
for j in range(len(ltArray) - 1):
ltArray[j] = ltArray[j + 1]
ltArray[len(ltArray) - 1] = firstValue
print('\nFinal Array Elements After Left Rotating')
print(ltArray)
Enter Position to Left Rotate Array Elements = 5
Original Array Elements Before Left Rotating
[19 25 32 77 28 99 44 88]
Final Array Elements After Left Rotating
[99 44 88 19 25 32 77 28]
在這個 Python 例子中,leftRotateArray 函數(shù)將把任意數(shù)組項旋轉(zhuǎn)到左手邊,printArrayItems 打印數(shù)組項。
import numpy as np
def printArrayItems(ltArray):
for i in ltArray:
print(i, end = ' ')
def leftRotateArray(ltArray, ltRotate):
for i in range(ltRotate):
firstValue = ltArray[0]
for j in range(len(ltArray) - 1):
ltArray[j] = ltArray[j + 1]
ltArray[len(ltArray) - 1] = firstValue
ltArray = np.random.randint(11, 110, size = 10)
ltRotate = int(input("\nEnter Position to Left Rotate Array Elements = "))
print('Original Array Elements Before Left Rotating')
printArrayItems(ltArray)
leftRotateArray(ltArray, ltRotate)
print('\nFinal Array Elements After Left Rotating')
printArrayItems(ltArray)
Enter Position to Left Rotate Array Elements = 7
Original Array Elements Before Left Rotating
73 45 65 97 84 99 40 18 77 105
Final Array Elements After Left Rotating
18 77 105 73 45 65 97 84 99 40
Python 程序使用 while 循環(huán)向左旋轉(zhuǎn) Numpy 數(shù)組 n。
import numpy as np
def printArrayItems(ltArray):
i = 0
while i < len(ltArray):
print(ltArray[i], end = ' ')
i = i + 1
def leftRotateArray(ltArray, ltRotate):
i = 0
while(i < ltRotate):
firstValue = ltArray[0]
j = 0
while(j < len(ltArray) - 1):
ltArray[j] = ltArray[j + 1]
j = j + 1
ltArray[len(ltArray) - 1] = firstValue
i = i + 1
ltArray = np.random.randint(70, 500, size = 15)
ltRotate = int(input("\nEnter Position to Left Rotate Array Elements = "))
print('Original Array Elements Before Left Rotating')
printArrayItems(ltArray)
leftRotateArray(ltArray, ltRotate)
print('\nFinal Array Elements After Left Rotating')
printArrayItems(ltArray)
Enter Position to Left Rotate Array Elements = 6
Original Array Elements Before Left Rotating
204 76 370 211 180 453 407 250 213 332 456 333 292 460 354
Final Array Elements After Left Rotating
407 250 213 332 456 333 292 460 354 204 76 370 211 180 453 網(wǎng)頁標(biāo)題:Python程序:將Numpy數(shù)組向左旋轉(zhuǎn)n
本文來源:http://www.5511xx.com/article/cddpijd.html


咨詢
建站咨詢
