列表去重
方法一:
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