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

相关文章

python实现指定字符串补全空格、前面填充0的方法

Python zfill()方法返回指定长度的字符串,原字符串右对齐,前面填充0。 zfill()方法语法:str.zfill(width) 参数width -- 指定字符串的长度。原字...

Pytorch Tensor的统计属性实例讲解

1. 范数 示例代码: import torch a = torch.full([8], 1) b = a.reshape([2, 4]) c = a.reshape([2, 2...

Python struct模块解析

Python struct模块解析

python提供了一个struct模块来提供转换。下面就介绍这个模块中的几个方法。     struct.pack(): struct.pack用于将Pyt...

Python从Excel中读取日期一列的方法

如下所示: import xlrd import datetime file=u"伏特加.xls"#注意读中文文件名稍微处理一下 data=xlrd.open_workbook(...

Python中的__SLOTS__属性使用示例

看python社区大妈组织的内容里边有一篇讲python内存优化的,用到了__slots__。然后查了一下,总结一下。感觉非常有用 python类在进行实例化的时候,会有一个__dict...