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

yipeiwu_com5年前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 openpyxl 遍历所有sheet 查找特定字符串的方法

如下所示: from openpyxl import workbook from openpyxl import load_workbook from openpyxl import...

基于python的列表list和集合set操作

基于python的列表list和集合set操作

以下是一些python的list和set的基本操作 1. list的一些操作 list = [1, 2, 3] list.append(5) print(list) list.e...

Python时间获取及转换知识汇总

 时间处理是我们日常开发中最最常见的需求,例如:获取当前datetime、获取当天date、获取明天/前N天、获取当天开始和结束时间(00:00:00 23:59:59)、获取...

python获得图片base64编码示例

  复制代码 代码如下: #!/usr/bin/env python # -*- coding: utf-8 -*- import os, base64 icon = open...

python 遍历列表提取下标和值的实例

如下所示: for index,value in enumerate(['apple', 'oppo', 'vivo']): print(index,value) 以上这篇py...