python实现将一个数组逆序输出的方法

yipeiwu_com6年前Python基础

方法一:

def printTheReverseArray(self): 
 list_1 = [1, 2, 3, 4, 5, 6, 7] 
 length = len(list_1) 
 for i in range(0, length): 
  print(length - i,end="") 

方法二:

def printTheReverseArray(self): 
 '头插法' 
 list_1 = [1, 2, 3, 4, 5, 6, 7] 
 list_2 = [list_1[0]]# 
 for i in range(1, len(list_1)): 
  list_2.insert(0, list_1[i]) 
 print(list_2) 

方法三:

数组前后交换-思想可以参考

def printTheReverseArray(self): 
 list_1 = [9, 6, 5, 4, 1] 
 N = len(list_1) 
 for i in range(int(len(list_1) / 2)): 
  list_1[i], list_1[N - i - 1] = list_1[N - i - 1], list_1[i] 
 print(list_1) 

以上这篇python实现将一个数组逆序输出的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python3 判断列表是一个空列表的方法

python3 判断空列表 @(python3) 有个判断列表是否为空的需求,试了好多方式,比如: a = [] if a is not None: COMMAND a =...

解决Pytorch 训练与测试时爆显存(out of memory)的问题

Pytorch 训练时有时候会因为加载的东西过多而爆显存,有些时候这种情况还可以使用cuda的清理技术进行修整,当然如果模型实在太大,那也没办法。 使用torch.cuda.empty_...

Pytorch 实现数据集自定义读取

以读取VOC2012语义分割数据集为例,具体见代码注释: VocDataset.py from PIL import Image import torch import torch....

解决python 输出是省略号的问题

这个问题非常非常重要,搞了一晚上都没解决好,但是真的很简单很简单, 如果你也 是用的numpy array, 如果你也想得到输出矩阵的全部内容,而不是省略形式, [[ 0.10284...

py2exe 编译ico图标的代码

复制代码 代码如下: #setup.py from distutils.core import setup import py2exe setup( # targets to build...