列表的方法(基础)python

  1. 查询功能:

查找指定元素在列表的下标,如果找不到,报错ValueRrror

语法:列表.index(元素)

list_name = ["python",666,6.66]
#查询666在列表中的下标
print(f"666在list_name中的下标为{list_name.index(666)}")
#若查找元素不存在则报错
print(f"444在list_name中的下标为{list_name.index(444)}")


666在list_name中的下标为1
Traceback (most recent call last):
  File "D:\programme\pychon\pythonProject\two\main.py", line 5, in <module>
    print(f"444在list_name中的下标为{list_name.index(444)}")
                               ^^^^^^^^^^^^^^^^^^^^
ValueError: 444 is not in list
  1. 修改功能:

修改特定位置的元素值

语法:列表[下标] = 值

list_name = ["python",666,6.66]
print(f"list_name[0] = {list_name[0]}")
#修改list_name中下标0的值
list_name[0] = "C"
print(f"list_name[0] = {list_name[0]}")



list_name[0] = python
list_name[0] = C
  1. 插入元素:

语法:列表.insert(下标,元素),在指定的下标位置,插入指定的元素

list_name = ["python",666,6.66]
print(f"list_name = {list_name}")
#在1的位置插入999
list_name.insert(1,999)
print(f"list_name = {list_name}")



list_name = ['python', 666, 6.66]
list_name = ['python', 999, 666, 6.66]
  1. 追加元素:

语法:列表.append(元素)

将指定元素,追加到列表的尾部

list_name = ["python",666,6.66]
print(f"list_name = {list_name}")
#在尾部的位置插入999
list_name.append(999)
print(f"list_name = {list_name}")



list_name = ['python', 666, 6.66]
list_name = ['python', 666, 6.66, 999]

追加不止一个元素:

语法:列表.extend(其他数据容器)

将其他数据容器的内容取出,一次追加到列表尾部

list_name = ["python",666,6.66]
list_name1 = ["C",999]
print(f"list_name = {list_name}")
#在尾部的位置插入list_name1
list_name.extend(list_name1)
print(f"list_name = {list_name}")



list_name = ['python', 666, 6.66]
list_name = ['python', 666, 6.66, 'C', 999]
  1. 删除元素

语法:del 列表[下标]

列表.pop(下标)

list_name = ["python",666,6.66]
print(f"list_name = {list_name}")
#删除位置1的元素
del list_name[1]
print(f"list_name = {list_name}")
#删除位置0的元素
list_name.pop(0)
print(f"list_name = {list_name}")



list_name = ['python', 666, 6.66]
list_name = ['python', 6.66]
list_name = [6.66]

删除元素在列表中的第一个匹配项

语法:列表.remove(元素)

list_name = ["python",666,6.66]
print(f"list_name = {list_name}")
#删除666元素
list_name.remove(666)
print(f"list_name = {list_name}")



list_name = ['python', 666, 6.66]
list_name = ['python', 6.66]
4.清空列表内容:

语法:列表.clear()

list_name = ["python",666,6.66]
print(f"list_name = {list_name}")
#清空列表
list_name.clear()
print(f"list_name = {list_name}")



list_name = ['python', 666, 6.66]
list_name = []

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

语法:列表.count(元素)

list_name = ["python",666,6.66,666,666]
print(f"list_name = {list_name}")
#统计666在列表中出现的次数
count=list_name.count(666)
print(f"666在list_name中出现的次数为{count}")



list_name = ['python', 666, 6.66, 666, 666]
666在list_name中出现的次数为3
  1. 统计列表内有多少元素

语法:len(列表)

list_name = ["python",666,6.66]
print(f"list_name = {list_name}")
#统计列表内有多少元素
len = len(list_name)
print(f"list_name列表中元素个数为:{len}")



list_name = ['python', 666, 6.66]
list_name列表中元素个数为:3

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值