5.5 判断指定某个元素是否在当前列表中
in 在当前的列表中
not in 不在当前的列表中
来进行判断是否在列表中,返回一个布尔值
5.6 列表数据的修改
运用赋值号‘=’进行数据修改
注意列表的元素修改时不能超过列表下标的最大值
5.7增加元素
append()
def append(self, *args, **kwargs): # real signature unknown
""" Append object to the end of the list. """
pass
注意点:
在列表的末尾追加一个元素,如果对象是列表,那就将列表作为一个元素追加
extend()
def extend(self, *args, **kwargs): # real signature unknown
""" Extend list by appending elements from the iterable. """
pass
注意点:
extend参数必须是可以迭代的,将元素迭代追加
index()
def index(self, *args, **kwargs): # real signature unknown
"""
Return first index of value.
Raises ValueError if the value is not present.
"""
pass
查找元素在列表中索引
查找不到数据会报错
insert()
def insert(self, *args, **kwargs): # real signature unknown
""" Insert object before index. """
pass
在列表的索引之前插入元素
5.8 删除数据
pop()
def pop(self, *args, **kwargs): # real signature unknown
"""
Remove and return item at index (default last).
Raises IndexError if list is empty or index is out of range.
"""
pass
删除并返回指定的元素(默认从最后开始)
如果列表为空或者索引值超过范围则引发一个Index错误
remove()
def remove(self, *args, **kwargs): # real signature unknown
"""
Remove first occurrence of value.
Raises ValueError if the value is not present.
"""
pass
删除第一个出现的值,如果在列表中找不到的值则返回一个Value错误
clear()
def clear(self, *args, **kwargs): # real signature unknown
""" Remove all items from list. """
pass
将列表中所有值进行清空,但不会删除列表
del 列表名
del list[-1]删除末位元素
将列表整体删除
del()
将列表删除
reverse()
def reverse(self, *args, **kwargs): # real signature unknown
""" Reverse *IN PLACE*. """
pass
将列表进行倒置
5.9 列表转字符串
str1 = ''.join([str(i) for i in list1])
def join(self, ab=None, pq=None, rs=None): # real signature unknown; restored from __doc__
"""
Concatenate any number of strings.
The string whose method is called is inserted in between each given string.
The result is returned as a new string.
Example: '.'.join(['ab', 'pq', 'rs']) -> 'ab.pq.rs'
"""
pass
5.10 zip()运用
zip()
class zip(object):
"""
zip(iter1 [,iter2 [...]]) --> zip object
Return a zip object whose .__next__() method returns a tuple where
the i-th element comes from the i-th iterable argument. The .__next__()
method continues until the shortest iterable in the argument sequence
is exhausted and then it raises StopIteration.
"""
相同下标的值压缩打包成一个元组组成的迭代器
5.11 enumerate()运用
class enumerate(object):
"""
Return an enumerate object.
iterable
an object supporting iteration
The enumerate object yields pairs containing a count (from start, which
defaults to zero) and a value yielded by the iterable argument.
enumerate is useful for obtaining an indexed list:
(0, seq[0]), (1, seq[1]), (2, seq[2]), ...
"""
注意点:
返回一个下标和对应的值所组成的元组组成的迭代器