Python 程序:以逆序打印数组元素

创新互联python教程:

创新互联服务项目包括锡林郭勒盟网站建设、锡林郭勒盟网站制作、锡林郭勒盟网页制作以及锡林郭勒盟网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,锡林郭勒盟网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到锡林郭勒盟省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!

编写一个 Python 程序,以逆序打印数组元素。在这个 Python 示例中,带有负值的列表切片以相反的顺序打印 numpy 数组元素。

import numpy as np

arr = np.array([15, 25, 35, 45, 55, 65, 75])

print('Printing the Array Elements in Reverse')
print(arr[::-1])

print('\nPrinting the Array Elements')
print(arr)

在这个 Python 程序中,for 循环根据索引位置从最后到第一次迭代数组元素,并以相反的顺序打印数组项。

import numpy as np

arr = np.array([15, 25, 35, 45, 55, 65, 75])

print('Printing the Array Elements in Reverse')
for i in range(len(arr) - 1, -1, -1):
    print(arr[i], end = '  ')
Printing the Array Elements in Reverse
75  65  55  45  35  25  15

Python 程序,使用 while 循环以相反的顺序打印数组元素。

import numpy as np

arr = np.array([22, 44, 66, 77, 89, 11, 19])

print('Printing the Array Elements in Reverse')
i = len(arr) - 1
while i >= 0:
    print(arr[i], end = '  ')
    i = i - 1
Printing the Array Elements in Reverse
19  11  89  77  66  44  22

这个 Python 示例允许输入 Numpy 数组元素并以相反的顺序打印它们。

import numpy as np

arrlist1 = []
arrTot = int(input("Total Number of Array Elements to enter = "))

for i in range(1, arrTot + 1):
    arrvalue = int(input("Please enter the %d Array Value = "  %i))
    arrlist1.append(arrvalue)

arr = np.array(arrlist1)

print('Printing the Array Elements in Reverse')
for i in range(len(arr) - 1, -1, -1):
    print(arr[i], end = '  ')
Total Number of Array Elements to enter = 9
Please enter the 1 Array Value = 21
Please enter the 2 Array Value = 34
Please enter the 3 Array Value = 65
Please enter the 4 Array Value = 78
Please enter the 5 Array Value = 98
Please enter the 6 Array Value = 34
Please enter the 7 Array Value = 65
Please enter the 8 Array Value = 87
Please enter the 9 Array Value = 99
Printing the Array Elements in Reverse
99  87  65  34  98  78  65  34  21

分享文章:Python 程序:以逆序打印数组元素
新闻来源:http://bzwzjz.com/article/dpcjepe.html

其他资讯

Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号
友情链接: 企业网站建设 成都企业网站制作 成都网站建设 手机网站制作 营销网站建设 泸州网站建设 成都网站建设 网站建设公司 成都网站建设 成都响应式网站建设 企业网站设计 定制网站建设多少钱 成都网站设计 温江网站设计 网站设计制作报价 成都商城网站制作 响应式网站设计 成都网站制作 营销型网站建设 网站建设方案 营销型网站建设 手机网站设计