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/n/331077.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
ZPHOMZPHOM
上一篇 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计算100以内所有奇数的和

    本文将从多个方面详细解释如何使用Python计算100以内所有奇数的和。 一、Python计算100以内所有奇数的和 Python可以通过for循环和条件判断来计算100以内所有奇…

    编程 2025-04-29
  • Python Set元素用法介绍

    Set是Python编程语言中拥有一系列独特属性及特点的数据类型之一。它可以存储无序且唯一的数据元素,这使得Set在数据处理中非常有用。Set能够进行交、并、差集等操作,也可以用于…

    编程 2025-04-29
  • Python计算1到n的奇数总和

    本文将介绍如何使用Python计算1到n的奇数总和,该算法对于初学Python编程的人员非常有帮助。 一、计算奇数总和的方法 计算1到n的奇数总和可以使用循环语句和条件语句实现。具…

    编程 2025-04-29

发表回复

登录后才能评论