Python 列表 基础知识

列表去重

方法一:

no_repeat_l = list(set(l)))

方法二:

no_repeat_l = {}.fromkeys(l).keys()

列表查找

操作一:判断值是否在/不在列表中 in/not in操作符


>>> list1 = [1, 2, 3, 4]
>>> a = 1
>>> a in list1
True
>>> a = 5
>>> a in list1
False
>>> a not in list1
True

操作二:统计指定值在列表中出现的次数 count方法

>>> list1 = [1, 2, 3, 4]
>>> a = 1
>>> list1.count(a)
1

操作三:查找指定值在列表中出现的位置 index方法
index方法返回查找对象的索引位置,并返回第一次出现的索引,如果没有找到对象则抛出异常。

str.index(str, beg=0, end=len(string))
str – 指定检索的字符串
beg – 开始索引,默认为0。
end – 结束索引,默认为字符串的长度

举例:

>>> list1 = [1, 2, 3, 4, 1]
>>> a = 1
>>> list1.index(a)
0
>>> list1.index(a, 1)
4
>> list1.index(a, 1, 3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: 1 is not in list
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值