python数组中倒数第一个元素怎么取

Python018

python数组中倒数第一个元素怎么取,第1张

python数组中打印出倒数第一个元素,方法如下:

# coding=utf-8  

''''' 

Created on 2015-7-8

@author: Administrator 

'''  

''''' 

创建数组 

'''  

arr = ["aex", "bfe", "mpilgrim", "zddd", "example"]  

''''' 

数组的负索引 

li[-n] == li[len(li) - n] 

'''  

print(arr[-1])#打印出arr数组中倒数第一个元素

工具/材料:电脑、Python、Pycharm

1、首先,打开Python,定义一个数组,其值为空。

2、接着,输入数组的长度,保存在变量中。

3、用for循环,控制输入数组的各元素。

4、从键盘逐一输入数组各元素,并添加到数组中。

5、使用函数,将数组倒序(逆序)排列。

6、最后,输出倒序(逆序)排序后的数组值。

7、运行程序,输入数组的各元素值后,电脑会将数组倒序(逆序)排列并输出。

with open('a.txt') as f:

    s=f.read()

L=[i.strip() for i in s.strip().split('\n')]

L2=[[int(i)for i in j.split(',')]   for j in L]

L2.sort(key=lambda s:s[-1])    #按最后一列排序

L3=[i[-3:] for i in L2]    #去除前几列,只保留后3列

with open('b.txt','w') as wf:

    wf.write('\n'.join([','.join(i) for i in L3]))