Python 程序:打印偶数位置数组元素

编写一个 Python 程序,打印偶数位置或偶数索引位置的数组元素。在这个 python 示例中,我们使用了递增 2 的列表切片来打印偶数位置的数组元素。

import numpy as np

arr = np.array([2, 4, 6, 8, 12, 14, 16, 18])

print('Printing the Array Elements at Even Position')
print(arr[1:len(arr):2])

使用 for 循环打印偶数索引位置上的数组元素的 Python 程序。

import numpy as np

arr = np.array([22, 44, 66, 88, 122, 144, 166, 188])

print('Printing the Array Elements at Even Position')
for i in range(1, len(arr), 2):
    print(arr[i], end = '  ')
Printing the Array Elements at Even Position
44  88  144  188 

Python 程序,使用 while 循环打印偶数位置的数组元素

import numpy as np

arr = np.array([11, 13, 15, 17, 19, 22, 39])

print('Printing the Array Elements at Even Position')
i = 1
while i < len(arr):
    print(arr[i], end = '  ')
    i = i + 2
Printing the Array Elements at Even Position
13  17  22 

在这个 Python 示例中,我们使用 if 语句来查找偶数索引位置,并打印偶数位置上存在的数组元素。

import numpy as np

evarrlist = []
evarrTot = int(input("Total Array Elements to enter = "))

for i in range(1, evarrTot + 1):
    evarrvalue = int(input("Please enter the %d Array Value = "  %i))
    evarrlist.append(evarrvalue)

evarr = np.array(evarrlist)

print('\nPrinting the Array Elements at Even Position')
for i in range(1, len(evarr), 2):
    print(evarr[i], end = '  ')

print('\nPrinting the Array Elements at Even Position')
for i in range(len(evarr)):
    if i % 2 != 0:
        print(evarr[i], end = '  ')
Total Array Elements to enter = 9
Please enter the 1 Array Value = 22
Please enter the 2 Array Value = 43
Please enter the 3 Array Value = 56
Please enter the 4 Array Value = 23
Please enter the 5 Array Value = 65
Please enter the 6 Array Value = 98
Please enter the 7 Array Value = 56
Please enter the 8 Array Value = 11
Please enter the 9 Array Value = 99

Printing the Array Elements at Even Position
43  23  98  11  
Printing the Array Elements at Even Position
43  23  98  11 

原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/246893.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
小蓝小蓝
上一篇 2024-12-12 13:17
下一篇 2024-12-12 13:18

相关推荐

  • 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函数 在介绍Python如何定义函数判断奇偶数之前,我们先来了解一下P…

    编程 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编程实现列表元素逆序存放

    本文将从以下几个方面对Python编程实现列表元素逆序存放做详细阐述: 一、实现思路 一般来说,使用Python将列表元素逆序存放可以通过以下几个步骤实现: 1. 定义一个列表 2…

    编程 2025-04-29

发表回复

登录后才能评论