判断指定的元素是否在当前的列表中
用 in 和 not in
list1= [1,2,3]
10 in list1
返回 false
列表数据修改
list1[0] = 0
下标不能越界
列表数据的操作
append 方法
list.append(object) 向列表中添加一个对象object
将所指定的object追加在末尾
只追加一个
extend方法
list.extend(sequence) 把一个序列seq的内容添加到列表中 参数必须是可迭代的
会将元素全取出来追加在末尾
index方法
index 查询值的索引
def index(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
"""
B.index(sub[, start[, end]]) -> int
Return the lowest index in B where subsection sub is found,
such that sub is contained within B[start,end]. Optional
arguments start and end are interpreted as in slice notation.
Raises ValueError when the subsection is not found.
"""
return 0
insert方法
insert添加数据在指定的索引值前 会改变原始数据
def insert(self, *args, **kwargs): # real signature unknown
"""
Insert a single item into the bytearray before the given index.
index
The index where the value is to be inserted.
item
The item to be inserted.
"""
pass
列表数据删除 remove
空列表或超出索引就报错
def remove(self, *args, **kwargs): # real signature unknown
"""
Remove the first occurrence of a value in the bytearray.
value
The value to remove.
"""
pass
列表数据提取 pop
删除并返回提取出的值
如果没给索引值就提取最后一个
若索引值超出列表大小报错
def pop(self, *args, **kwargs): # real signature unknown
"""
Remove and return a single item from B.
index
The index from where to remove the item.
-1 (the default value) means remove the last item.
If no index argument is given, will pop the last item.
"""
pass
clear方法
clear 清除列表中所有数据
def clear(self): # real signature unknown; restored from __doc__
""" D.clear() -> None. Remove all items from D. """
pass
del方法
del 未指定下标就删除所有对象
用法
del list1
del list1[2]
列表转字符串
join方法
join参数要是可迭代对象并且是字符串
例1
list1 = [‘a’,‘b’,‘c’] 可以
str = ‘’.join(list1)
list1 = [1,2,3,4] 不行
例2
str = ‘’.join([str(i) for i in list1])
zip 方法 压缩
将相同下标的内容打包成元组
用for循环遍历
元组:不可修改的值
例:
list1 = [1,2,3,4,5,6,7,8]
list2 = [1,2,3,4,5,6,7,8]
str1 = zip(list1,list2)
for i in str1:
print(i)
enumerate 方法 枚举函数
enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中。
seasons = ['Spring', 'Summer', 'Fall', 'Winter']
list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
list(enumerate(seasons, start=1)) # 下标从 1 开始
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
参数需要时可迭代对象
返回值是迭代器 前面是索引后面是值
3479

被折叠的 条评论
为什么被折叠?



