博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python3—列表
阅读量:5135 次
发布时间:2019-06-13

本文共 1996 字,大约阅读时间需要 6 分钟。

列表是什么

name = ["jim", "lilei", "lucy"]     #方括号表示,逗号分隔print(name)print(name[0])print(name[0].title())

输出:

['jim', 'lilei', 'lucy']

jim
Jim

name = ["jim", "lilei", "lucy"]print(name[0], name[1], name[2])print(name[-1], name[-2], name[-3])   #-1索引表示最后一个元素

输出:

jim lilei lucy

lucy lilei jim

 

name = ["jim", "lilei", "lucy"]name[0] = 'lily'  #修改第一个元素print(name)#添加元素name.append('xiaoming')   #在末尾添加print(name)name.insert(1,'ouyangfeng')   #在name[1]处添加,原来的name[1]以及以后的后移print(name)del name[1]  #删除name[1],以后不用name[1]了print(name)delete_end = name.pop()print(delete_end, name)delete_name = name.pop(1)  #删除name[1],以后还可以用print("delete_name: ",delete_name)print(name)name.remove('lucy')  #删除lucyprint(name)

输出:

['lily', 'lilei', 'lucy']

['lily', 'lilei', 'lucy', 'xiaoming']
['lily', 'ouyangfeng', 'lilei', 'lucy', 'xiaoming']
['lily', 'lilei', 'lucy', 'xiaoming']
xiaoming ['lily', 'lilei', 'lucy']
delete_name:  lilei
['lily', 'lucy']
['lily']

 

永久排序:

cars =['dazhong','Baoma','aodi','qq','Qirui']cars.sort() #按字母从小到大print(cars)cars.sort(reverse=True)  #按字母从大到小print(cars)

 

output:

['Baoma', 'Qirui', 'aodi', 'dazhong', 'qq']

['qq', 'dazhong', 'aodi', 'Qirui', 'Baoma']

 

临时排序:

cars =['dazhong','Baoma','aodi','qq','Qirui']tmp_sort = sorted(cars)print("tmp_sort:",tmp_sort)print("origianl:",cars)print("large->small:",sorted(cars,reverse=True))print("origianl:",cars)

output:

tmp_sort: ['Baoma', 'Qirui', 'aodi', 'dazhong', 'qq']

origianl: ['dazhong', 'Baoma', 'aodi', 'qq', 'Qirui']
large->small: ['qq', 'dazhong', 'aodi', 'Qirui', 'Baoma']
origianl: ['dazhong', 'Baoma', 'aodi', 'qq', 'Qirui']

 

反转:

cars =['dazhong','Baoma','aodi','qq','Qirui']print(cars)cars.reverse()print(cars)cars.reverse()print("recover: ", cars)

output:

['dazhong', 'Baoma', 'aodi', 'qq', 'Qirui']

['Qirui', 'qq', 'aodi', 'Baoma', 'dazhong']
recover:  ['dazhong', 'Baoma', 'aodi', 'qq', 'Qirui']

cars =['dazhong','Baoma','aodi','qq','Qirui']length = len(cars)print(length)

output:

5

 

转载于:https://www.cnblogs.com/crazybird123/p/7524188.html

你可能感兴趣的文章
Nhibernate 过长的字符串报错 dehydration property
查看>>
Deque - leetcode 【双端队列】
查看>>
gulp插件gulp-ruby-sass和livereload插件
查看>>
免费的大数据学习资料,这一份就足够
查看>>
clientWidth、clientHeight、offsetWidth、offsetHeight以及scrollWidth、scrollHeight
查看>>
企业级应用与互联网应用的区别
查看>>
itext jsp页面打印
查看>>
Perl正则表达式匹配
查看>>
DB Change
查看>>
nginx --rhel6.5
查看>>
Eclipse Python插件 PyDev
查看>>
selenium+python3模拟键盘实现粘贴、复制
查看>>
第一篇博客
查看>>
网站搭建(一)
查看>>
SDWebImage源码解读之SDWebImageDownloaderOperation
查看>>
elastaticsearch
查看>>
postgreSQL 简单命令操作
查看>>
Spring JDBCTemplate
查看>>
Radon变换——MATLAB
查看>>
Iroha and a Grid AtCoder - 1974(思维水题)
查看>>