Python(六) 列表

编程入门 行业动态 更新时间:2024-10-20 13:52:47

Python(六) <a href=https://www.elefans.com/category/jswz/34/1771047.html style=列表"/>

Python(六) 列表

Python 中没有数组,而是加入了功能更强大的列表(list),列表可以存储任何类型的数据。

使用

访问
a = [1,2.3,"python"]
print('a[0] -->', a[0])
print('a[1:] -->', a[1:])
输出
a[0] --> 1
a[1:] --> [2.3, 'python']
更新
a = [1,2.3,"python"]
a[1] = '更新'
print('a更新后--->',a)#输出
a更新后---> [1, '更新', 'python']
'''
更新
还可以使用 append() 向列表中添加新元素
'''
a = [1,2.3,"python"]
a.append('hello')
print('a--->',a)#输出
a---> [1, '2.3', 'python', 'hello']
删除
a = [1, 2.3, 'python', 'hello']
del a[2]
print('a--->',a)#输出
a---> [1, 2.3, 'hello']
常用函数
len()列表元素个数

count()

统计列表中某个元素出现的次数

index()

查找某个元素在列表中首次出现的位置(即索引)

remove()

移除列表中某个值的首次匹配项

sort()

对列表中元素进行排序

copy()

复制列表
clear()清空列表
append()追加元素
l = ['d', 'b', 'a', 'f', 'd']
print(len(l))
print("l.count('d') -->", l.count('d'))print("l.index('d') -->", l.index('d'))l.remove('d')
print("l -->", l)l.sort()
print('l -->', l)lc = l.copy()
print('lc -->', lc)lc.append('aaa')
print(lc)lc.clear()
print('clear--->',lc)#输出
5
l.count('d') --> 2
l.index('d') --> 0
l --> ['b', 'a', 'f', 'd']
l --> ['a', 'b', 'd', 'f']
lc --> ['a', 'b', 'd', 'f']
['a', 'b', 'd', 'f', 'aaa']
clear---> []

更多推荐

Python(六) 列表

本文发布于:2023-11-15 11:03:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1598573.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:列表   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!