list简单使用记录

创建一个列表

list1 = ['a', 'b', 'c', 4, 5, 6]

列表中增加元素(增)

列表末尾增加元素
list1.append(8)
list1
    ['a', 'b', 'c', 4, 5, 6, 8]
列表指定位置增加元素
list1.insert(1, 'f')
list1
    ['hello world', 'f', 'f', 'b', 'c', 4, 5, 6, 8]

列表中修改元素(改)

list1[0] = 'hello world'
list1
    ['hello world', 'f', 'b', 'c', 4, 5, 6, 8]

列表中删除元素(删)

del list[-1]
list1
    ['hello world', 'f', 'b', 'c', 4, 5, 6, 8]
list1.remove('b')
list1
    ['hello world', 'c', 4, 5, 6, 8]
list1.remove(list1[2])
list1
    ['hello world', 'c', 5, 6, 8]
list1.pop(1)
    'f'
list1
    ['hello world', 'c', 5, 6, 8]

remove后索引发生了变化元素被删除
pop后已赋值的元素还在且可方便寻找

pop_ele = list1.pop(2)
pop_ele
    5
list1
    ['hello world', 'c', 6, 8]

列表元素查找(查)

list1[2]
    6
list1[-1]
    8
list1[0:2]
    ['hello world', 'c']
list2 = [i for i in range(10)]
list2
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
list2[0:9:3]
    [0, 3, 6]
list2[-9:-1:2]
    [1, 3, 5, 7]
***前两个数字为查询范围,最后一个数字为间隔,都为从左往右***
len(list2)
    10
3 in list2
   True
11 in list2
    False
***通过len来查找元素是否在列表中***
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值