您现在的位置是:课程教程文章
Python之列表的增删改查
2023-12-18 23:33课程教程文章 人已围观
-
基于GIS的Python开发应用(GIS思维)
基于GIS的Python开发应用(GIS思维)本视频教程将持续分享《基于GIS的Python开发应用》,不定期分享,以实际项目需... -
Python实战一(爬虫+多线程+数据库)
Python实战一(爬虫+多线程+数据库)1.内容详实,纯干货; 2.靠谱的实战示例; 3.初学者即可轻松驾驭; 4.爬虫+多线... -
Python+Selenium + Webdriver元素定位 最基础的自
Python+Selenium + Webdriver元素定位 最基础的自... -
Python入门到精通/机器学习/爬虫/数据分析
Python入门到精通/机器学习/爬虫/数据分析...
列表的查
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']
相关推荐:《Python视频教程》
列表的改
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']
上一篇:Python itertools模块:生成迭代器(实例分析)
下一篇:没有了