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

成都創(chuàng)新互聯(lián)公司長(zhǎng)期為超過(guò)千家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開(kāi)放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為南海企業(yè)提供專業(yè)的成都做網(wǎng)站、成都網(wǎng)站制作,南海網(wǎng)站改版等技術(shù)服務(wù)。擁有10年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開(kāi)發(fā)。
編寫一個(gè) Python 程序,打印奇數(shù)位置或奇數(shù)索引位置的數(shù)組元素。在這個(gè) Python 示例中,列表切片從 0 開(kāi)始,然后遞增 2。
import numpy as np
arr = np.array([1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21])
print('Printing the Array Elements at Odd Position')
print(arr[0:len(arr):2])
Printing the Array Elements at Odd Position
[ 1 5 9 13 17 21]使用 for 循環(huán)打印奇數(shù)索引位置的數(shù)組元素的 Python 程序。
import numpy as np
arr = np.array([10, 25, 20, 33, 40, 55, 60, 77])
print('Printing the Array Elements at Even Position')
for i in range(0, len(arr), 2):
print(arr[i], end = ' ')
Python 程序,使用 while 循環(huán)打印奇數(shù)位置的數(shù)組元素。
import numpy as np
odarr = np.array([3, 9, 11, 4, 22, 8, 99, 19, 7])
print('Printing the Array Elements at Odd Position')
i = 0
while i < len(odarr):
print(odarr[i], end = ' ')
i = i + 2
Printing the Array Elements at Odd Position
3 11 22 99 7 在這個(gè) Python 示例中,if 語(yǔ)句(如果 i % 2 == 0)找到奇數(shù)索引位置,并打印出現(xiàn)在奇數(shù)位置的數(shù)組元素。
import numpy as np
odarrlist = []
odarrTot = int(input("Total Array Elements to enter = "))
for i in range(1, odarrTot + 1):
odarrvalue = int(input("Please enter the %d Array Value = " %i))
odarrlist.append(odarrvalue)
odarr = np.array(odarrlist)
print('Printing the Array Elements at Odd Position')
for i in range(0, len(odarr), 2):
print(odarr[i], end = ' ')
print('\nPrinting the Array Elements at Odd Position')
for i in range(len(odarr)):
if i % 2 == 0:
print(odarr[i], end = ' ')
Total Array Elements to enter = 9
Please enter the 1 Array Value = 17
Please enter the 2 Array Value = 22
Please enter the 3 Array Value = 33
Please enter the 4 Array Value = 45
Please enter the 5 Array Value = 56
Please enter the 6 Array Value = 76
Please enter the 7 Array Value = 78
Please enter the 8 Array Value = 89
Please enter the 9 Array Value = 90
Printing the Array Elements at Odd Position
17 33 56 78 90
Printing the Array Elements at Odd Position
17 33 56 78 90 分享題目:Python程序:打印奇數(shù)位置數(shù)組元素
文章起源:http://www.dlmjj.cn/article/dpsejhg.html


咨詢
建站咨詢
