列表
1. 表达形式:
[] 或 list()
class list(object):
"""
list() -> new empty list
list(iterable) -> new list initialized from iterable's items
"""
2. 元素
可以是不同的数据类型
3. 嵌套
列表可以嵌套列表
4. 可迭代对象
列表是一个可迭代对象:可以使用for语句取出每个元素
判断对象是否为可迭代对象:
from collections import Iterable
isinstance(对象,类)
5. 列表的基本操作
1. 增
1. list.append(self, p_object)
def append(self, p_object): # real signature unknown; restored from __doc__
""" L.append(object) -> None -- append object to end """
pass
功能:在列表末尾增加一个元素,该元素是一个对象。
2. list.extend(self, iterable)
def extend(self, iterable): # real signature unknown; restored from __doc__
""" L.extend(iterable) -> None -- extend list by appending elements from the iterable """
pass
功能:将可迭代对象中的元素挨个取出加入到列表中。
2. 删
1. list.remove(self, value)
def remove(self, value): # real signature unknown; restored from __doc__
"""
L.remove(value) -> None -- remove first occurrence of value.
Raises ValueError if the value is not present.
"""
pass
功能:将列表中第一次出现的元素值删除。
2. list.clear(self)
def clear(self): # real signature unknown; restored from __doc__
""" L.clear() -> None -- remove all items from L """
pass
功能:将列表的内容清空。
3. 内置函数:del list
功能:将整个列表删除。
3. 查
list.index()
def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__
"""
L.index(value, [start, [stop]]) -> integer -- return first index of value.
Raises ValueError if the value is not present.
"""
return 0
功能:获取第一个指定值的索引。
list.pop(self, index=None)
def pop(self, index=None): # real signature unknown; restored from __doc__
"""
L.pop([index]) -> item -- remove and return item at index (default last).
Raises IndexError if list is empty or index is out of range.
"""
pass
功能:将指定索引处的内容删除并返回,默认最后一个。
4. 改
5. 逆置
1. 内置函数reversed(object)
class reversed(object):
"""
reversed(sequence) -> reverse iterator over values of the sequence
Return a reverse iterator
"""
功能:将序列的值逆置,并返回一个迭代器。
2. list.reverse(self)
def reverse(self): # real signature unknown; restored from __doc__
""" L.reverse() -- reverse *IN PLACE* """
pass
功能:将列表中的元素逆置,在当前数据上操作。
6. 排序
1. 内置函数sorted(*args, **kwargs)
def sorted(*args, **kwargs): # real signature unknown
"""
Return a new list containing all items from the iterable in ascending order.
A custom key function can be supplied to customize the sort order, and the
reverse flag can be set to request the result in descending order.
"""
pass
功能:1. 将迭代器的内容按升序排序,返回一个新的列表。
2. 可以提供自定义键功能来自定义排序顺序,以及可以设置反向标志以按降序请求结果。
3. 注:*args表示任何多个无名参数,它是一个tuple;**kwargs表示关键字参数,它是一个dict。并且同时使用*args和**kwargs时,必须*args参数列要在**kwargs前。
2. list.sort(self, key=None, reverse=False)
def sort(self, key=None, reverse=False): # real signature unknown; restored from __doc__
""" L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE* """
pass
功能:将原始列表中的数据排序,默认从小到大。