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设计】。

相关文章

easy_install python包安装管理工具介绍

easy_install更准确的说是一个和setuptools绑定的模块,一切下载、构建、安装和管理的工作都可以由它来担当。 一般的执行方式: easy_install + URL 但是...

padas 生成excel 增加sheet表的实例

padas 生成excel 增加sheet表的实例

基本介绍 pandas是Python数据挖掘、数据分析中常用的库。而DataFrame生成excel中的sheet表,以及在excel中增加sheet表,在数据分 析中也经常用到。这里以...

基于Python log 的正确打开方式

保存代码到文件:logger.py import os import logbook from logbook.more import ColorizedStderrHandler...

python用ConfigObj读写配置文件的实现代码

发现一个简单而又强大的读写配置文件的lib,http://www.voidspace.org.uk/python/configobj.html。个人觉得最大的亮点在于自带的格式校验功能,...

python实现在目录中查找指定文件的方法

本文实例讲述了python实现在目录中查找指定文件的方法。分享给大家供大家参考。具体实现方法如下: 1. 模糊查找 复制代码 代码如下:import os from glob impor...