python列表的方法

语法: 

        

方法:

     1、按照下标和切片取值

     

增加:

        1、append("数据") 在列表最后面增加
        2、insert(下标,数据)在指定的位置插入数据
        3、extend(数据);他是迭代式追加,要是字符串会一个一个字符追加,要是列表会一个一个元素追加

       

删除:

               1、remove("123")根据数据删除,如果有重名只删除第一个
               2、pop(-2);根据小标删除元素,返回删除的元素默认删除最后一个
               3、clear();清空元素
               4、del 列表名[-1:-6:-1]可以按照索引删除,也可以按照切片来删除

        

改: 列表[下标]="数据";根据下标修改值

       

 查询: 

      

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python中常用的列表方法有很多,以下是其中几个常用的方法: 1. append()方法:在列表末尾添加新的对象。 ```python list1 = ['hello', 'world', 'python'] list1.append('...') print(list1) # 输出:['hello', 'world', 'python', '...'] ``` 2. extend()方法:在列表末尾一次性追加另一个序列中的多个值。 ```python list1 = ['hello', 'world', 'python'] list2 = ['!', 'I', 'love', 'Python'] list1.extend(list2) print(list1) # 输出:['hello', 'world', 'python', '!', 'I', 'love', 'Python'] ``` 3. insert()方法:将对象插入到列表中的指定位置。 ```python list1 = ['hello', 'world', 'python'] list1.insert(1, 'beautiful') print(list1) # 输出:['hello', 'beautiful', 'world', 'python'] ``` 4. remove()方法:移除列表中某个值的第一个匹配项。 ```python list1 = ['hello', 'world', 'python'] list1.remove('world') print(list1) # 输出:['hello', 'python'] ``` 5. pop()方法:移除列表中的一个元素(默认最后一个元素),并且返回该元素的值。 ```python list1 = ['hello', 'world', 'python'] last_element = list1.pop() print(last_element) # 输出:'python' print(list1) # 输出:['hello', 'world'] ``` 6. sort()方法:对列表进行排序。 ```python list1 = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5] list1.sort() print(list1) # 输出:[1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9] ``` 7. count()方法:统计某个元素在列表中出现的次数。 ```python list1 = ['hello', 'world', 'python', 'world'] count = list1.count('world') print(count) # 输出:2 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值