Python 列表增删改查排序统计
2018-06-23 13:30:44来源:未知 阅读 ()
列表的查
a = ["klvchen", "tom", "jack", "james", "lily", "lucy"] print(a[1:4])
结果:
['tom', 'jack', 'james']
注意:列表的切片的下标以 0 为开始。即: 下标0 --> klvchen,下标1 --> tom,..... 下标5 --> lucy。
列表的切换顾头不顾尾,例如:a [1:4],则不会取到列表下标为4的元素
从左到右,取到最后:
a = ["klvchen", "tom", "jack", "james", "lily", "lucy"] print(a[1:])
结果:
['tom', 'jack', 'james', 'lily', 'lucy']
从左到右,取到倒数第二个值:
a = ["klvchen", "tom", "jack", "james", "lily", "lucy"] print(a[1:-1])
结果:
['tom', 'jack', 'james', 'lily']
从左到右,隔一个去取:
a = ["klvchen", "tom", "jack", "james", "lily", "lucy"] print(a[1::2])
结果:
['tom', 'james', 'lucy']
从右到左,一个一个去取:
a = ["klvchen", "tom", "jack", "james", "lily", "lucy"] print(a[3::-1])
结果:
['james', 'jack', 'tom', 'klvchen']
从右到左去取
a = ["klvchen", "tom", "jack", "james", "lily", "lucy"] print(a[-2:-4:-1])
结果:
['lily', 'james']
列表的增
append 方法默认插到最后一个位置
a = ["klvchen", "tom", "jack", "james", "lily", "lucy"] a.append("mary") print(a) 结果: ['klvchen', 'tom', 'jack', 'james', 'lily', 'lucy', 'mary']
insert 方法将数据插入到任意一个位置
a = ["klvchen", "tom", "jack", "james", "lily", "lucy"] a.insert(1,"mary") print(a) 结果: ['klvchen', 'mary', 'tom', 'jack', 'james', 'lily', 'lucy']
extend 方法可在列表的末尾一次性追加另一个序列中的多个值
a = ["klvchen", "tom", "jack"] b = ["james", "lily", "lucy"] a.extend(b) print(a)
结果:
['klvchen', 'tom', 'jack', 'james', 'lily', 'lucy']
列表的改
a = ["klvchen", "tom", "jack", "james", "lily", "lucy"] a[1]="tom2" print(a) 结果: ['klvchen', 'tom2', 'jack', 'james', 'lily', 'lucy']
a = ["klvchen", "tom", "jack", "james", "lily", "lucy"] a[1:3]=["tom2", "jack2"] print(a) 结果: ['klvchen', 'tom2', 'jack2', 'james', 'lily', 'lucy']
列表的删
remove 方法
a = ["klvchen", "tom", "jack", "james", "lily", "lucy"] a.remove("tom") print(a) 结果: ['klvchen', 'jack', 'james', 'lily', 'lucy']
pop 方法
a = ["klvchen", "tom", "jack", "james", "lily", "lucy"] b = a.pop(1) print(a) print(b) 结果: ['klvchen', 'jack', 'james', 'lily', 'lucy'] tom
注意 pop 方法会返回删除的元素
del 方法
a = ["klvchen", "tom", "jack", "james", "lily", "lucy"] del a[1] print(a) 结果: ['klvchen', 'jack', 'james', 'lily', 'lucy']
a = ["klvchen", "tom", "jack", "james", "lily", "lucy"] del a print(a) 结果: NameError: name 'a' is not defined
注意:del a 把列表a从内存删除了,所以打印 a 列表程序会报错
列表元素的统计
list = ['a', 'a', 'b', 'c'] res = list.count('a') print(res) 结果: 2
列表的排序
sort 方法
a = ["klvchen", "tom", "jack", "james", "lily", "lucy"] print(a) a.sort() print(a) 结果: ['klvchen', 'tom', 'jack', 'james', 'lily', 'lucy'] ['jack', 'james', 'klvchen', 'lily', 'lucy', 'tom']
reverse 方法
a = ["klvchen", "tom", "jack", "james", "lily", "lucy"] print(a) a.reverse() print(a) 结果: ['klvchen', 'tom', 'jack', 'james', 'lily', 'lucy'] ['lucy', 'lily', 'james', 'jack', 'tom', 'klvchen']
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- python3基础之“术语表(2)” 2019-08-13
- python3 之 字符串编码小结(Unicode、utf-8、gbk、gb2312等 2019-08-13
- Python3安装impala 2019-08-13
- 小白如何入门 Python 爬虫? 2019-08-13
- python_字符串方法 2019-08-13
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash