Python 程序:打印奇數位置數組元素

編寫一個 Python 程序,打印奇數位置或奇數索引位置的數組元素。在這個 Python 示例中,列表切片從 0 開始,然後遞增 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 循環打印奇數索引位置的數組元素的 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 循環打印奇數位置的數組元素。

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 

在這個 Python 示例中,if 語句(如果 i % 2 == 0)找到奇數索引位置,並打印出現在奇數位置的數組元素。

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 

原創文章,作者:ZPHOM,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/331407.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
ZPHOM的頭像ZPHOM
上一篇 2025-01-16 15:46
下一篇 2025-01-16 15:46

相關推薦

  • ArcGIS更改標註位置為中心的方法

    本篇文章將從多個方面詳細闡述如何在ArcGIS中更改標註位置為中心。讓我們一步步來看。 一、禁止標註智能調整 在ArcMap中設置標註智能調整可以自動將標註位置調整到最佳顯示位置。…

    編程 2025-04-29
  • Python導入數組

    本文將為您詳細闡述Python導入數組的方法、優勢、適用場景等方面,並附上代碼示例。 一、numpy庫的使用 numpy是Python中一個強大的數學庫,其中提供了非常豐富的數學函…

    編程 2025-04-29
  • Python返回數組:一次性搞定多種數據類型

    Python是一種多用途的高級編程語言,具有高效性和易讀性的特點,因此被廣泛應用於數據科學、機器學習、Web開發、遊戲開發等各個領域。其中,Python返回數組也是一項非常強大的功…

    編程 2025-04-29
  • Python遍歷集合中的元素

    本文將從多個方面詳細闡述Python遍歷集合中的元素方法。 一、for循環遍歷集合 Python中,使用for循環可以遍歷集合中的每個元素,代碼如下: my_set = {1, 2…

    編程 2025-04-29
  • Python去掉數組的中括號

    在Python中,被中括號包裹的數據結構是列表,列表是Python中非常常見的數據類型之一。但是,有些時候我們需要將列表展開成一維的數組,並且去掉中括號。本文將為大家詳細介紹如何用…

    編程 2025-04-29
  • Python操作數組

    本文將從多個方面詳細介紹如何使用Python操作5個數組成的列表。 一、數組的定義 數組是一種用於存儲相同類型數據的數據結構。Python中的數組是通過列表來實現的,列表中可以存放…

    編程 2025-04-29
  • Python列表中大於某數的元素處理方法

    本文將會介紹如何在Python列表中找到大於某數的元素,並對其進行進一步的處理。 一、查找大於某數的元素 要查找Python列表中大於某數的元素,可以使用列表推導式進行處理。 nu…

    編程 2025-04-29
  • Python Set元素用法介紹

    Set是Python編程語言中擁有一系列獨特屬性及特點的數據類型之一。它可以存儲無序且唯一的數據元素,這使得Set在數據處理中非常有用。Set能夠進行交、並、差集等操作,也可以用於…

    編程 2025-04-29
  • 用Python計算100以內所有奇數的和

    本文將從多個方面詳細解釋如何使用Python計算100以內所有奇數的和。 一、Python計算100以內所有奇數的和 Python可以通過for循環和條件判斷來計算100以內所有奇…

    編程 2025-04-29
  • Python計算1到n的奇數總和

    本文將介紹如何使用Python計算1到n的奇數總和,該算法對於初學Python編程的人員非常有幫助。 一、計算奇數總和的方法 計算1到n的奇數總和可以使用循環語句和條件語句實現。具…

    編程 2025-04-29

發表回復

登錄後才能評論