python:list对象的全部详细行为(每一个都有详细讲解)保证一看就会

python:list类型中所有的方法,每一种方法附带一个实例:以及解释说明

list类型中所有的方法(行为)

获取方式

通过help函数获取

实例
help(list)

# 输出内容
Help on class list in module builtins:

class list(object)
 |  list(iterable=(), /)
 |  
 |  Built-in mutable sequence.
 |  
 |  If no argument is given, the constructor creates a new empty list.
 |  The argument must be an iterable if specified.
 |  
 |  Methods defined here:
 |  
 |  __add__(self, value, /)
 |      Return self+value.
 |  
 |  __contains__(self, key, /)
 |      Return key in self.
 |  
 |  __delitem__(self, key, /)
 |      Delete self[key].
 |  
 |  __eq__(self, value, /)
 |      Return self==value.
 |  
 |  __ge__(self, value, /)
 |      Return self>=value.
 |  
 |  __getattribute__(self, name, /)
 |      Return getattr(self, name).
 |  
 |  __getitem__(...)
 |      x.__getitem__(y) <==> x[y]
 |  
 |  __gt__(self, value, /)
 |      Return self>value.
 |  
 |  __iadd__(self, value, /)
 |      Implement self+=value.
 |  
 |  __imul__(self, value, /)
 |      Implement self*=value.
 |  
 |  __init__(self, /, *args, **kwargs)
 |      Initialize self.  See help(type(self)) for accurate signature.
 |  
 |  __iter__(self, /)
 |      Implement iter(self).
 |  
 |  __le__(self, value, /)
 |      Return self<=value.
 |  
 |  __len__(self, /)
 |      Return len(self).
 |  
 |  __lt__(self, value, /)
 |      Return self<value.
 |  
 |  __mul__(self, value, /)
 |      Return self*value.
 |  
 |  __ne__(self, value, /)
 |      Return self!=value.
 |  
 |  __repr__(self, /)
 |      Return repr(self).
 |  
 |  __reversed__(self, /)
 |      Return a reverse iterator over the list.
 |  
 |  __rmul__(self, value, /)
 |      Return value*self.
 |  
 |  __setitem__(self, key, value, /)
 |      Set self[key] to value.
 |  
 |  __sizeof__(self, /)
 |      Return the size of the list in memory, in bytes.
 |  
 |  append(self, object, /)
 |      Append object to the end of the list.
 |  
 |  clear(self, /)
 |      Remove all items from list.
 |  
 |  copy(self, /)
 |      Return a shallow copy of the list.
 |  
 |  count(self, value, /)
 |      Return number of occurrences of value.
 |  
 |  extend(self, iterable, /)
 |      Extend list by appending elements from the iterable.
 |  
 |  index(self, value, start=0, stop=9223372036854775807, /)
 |      Return first index of value.
 |      
 |      Raises ValueError if the value is not present.
 |  
 |  insert(self, index, object, /)
 |      Insert object before index.
 |  
 |  pop(self, index=-1, /)
 |      Remove and return item at index (default last).
 |      
 |      Raises IndexError if list is empty or index is out of range.
 |  
 |  remove(self, value, /)
 |      Remove first occurrence of value.
 |      
 |      Raises ValueError if the value is not present.
 |  
 |  reverse(self, /)
 |      Reverse *IN PLACE*.
 |  
 |  sort(self, /, *, key=None, reverse=False)
 |      Sort the list in ascending order and return None.
 |      
 |      The sort is in-place (i.e. the list itself is modified) and stable (i.e. the
 |      order of two equal elements is maintained).
 |      
 |      If a key function is given, apply it once to each list item and sort them,
 |      ascending or descending, according to their function values.
 |      
 |      The reverse flag can be set to sort in descending order.
 |  
 |  ----------------------------------------------------------------------
 |  Class methods defined here:
 |  
 |  __class_getitem__(...) from builtins.type
 |      See PEP 585
 |  
 |  ----------------------------------------------------------------------
 |  Static methods defined here:
 |  
 |  __new__(*args, **kwargs) from builtins.type
 |      Create and return a new object.  See help(type) for accurate signature.
 |  
 |  ----------------------------------------------------------------------
 |  Data and other attributes defined here:
 |  
 |  __hash__ = None

这里有所有关于list的行为或者方法,我们这里选取这些行为

 |  append(self, object, /)
 |      Append object to the end of the list.
 |  
 |  clear(self, /)
 |      Remove all items from list.
 |  
 |  copy(self, /)
 |      Return a shallow copy of the list.
 |  
 |  count(self, value, /)
 |      Return number of occurrences of value.
 |  
 |  extend(self, iterable, /)
 |      Extend list by appending elements from the iterable.
 |  
 |  index(self, value, start=0, stop=9223372036854775807, /)
 |      Return first index of value.
 |      
 |      Raises ValueError if the value is not present.
 |  
 |  insert(self, index, object, /)
 |      Insert object before index.
 |  
 |  pop(self, index=-1, /)
 |      Remove and return item at index (default last).
 |      
 |      Raises IndexError if list is empty or index is out of range.
 |  
 |  remove(self, value, /)
 |      Remove first occurrence of value.
 |      
 |      Raises ValueError if the value is not present.
 |  
 |  reverse(self, /)
 |      Reverse *IN PLACE*.
 |  
 |  sort(self, /, *, key=None, reverse=False)
 |      Sort the list in ascending order and return None.
 |      
 |      The sort is in-place (i.e. the list itself is modified) and stable (i.e. the
 |      order of two equal elements is maintained).
 |      
 |      If a key function is given, apply it once to each list item and sort them,
 |      ascending or descending, according to their function values.
 |      
 |      The reverse flag can be set to sort in descending order.
详细解释(每一个都带有详细讲解)
append

解释内容

  • append(self, object, /)
    Append object to the end of the list.

参数

  • self:默认参数,表示是当前从类中实例化出来的对象
  • object:添加的对象
  • /:限制参数,表示在/之前的参数都是仅位置参数

作用

  • Append object to the end of the list.
  • 添加对象到list的末尾
clear

解释内容

  • clear(self, /)
    Remove all items from list.

参数

  • self:默认参数,表示是当前从类中实例化出来的对象
  • /:限制参数,表示在/之前的参数都是仅位置参数

作用

  • Remove all items from list.
  • 清除list中的全部元素
copy

解释内容

  • copy(self, /)
    Return a shallow copy of the list.

参数

  • self:默认参数,表示是当前从类中实例化出来的对象
  • /:限制参数,表示在/之前的参数都是仅位置参数

作用

  • Return a shallow copy of the list.
  • 返回list的浅拷贝副本

注意:

这里浅拷贝的意思为 直接把在原list上面进行拷贝 遇到list嵌套list的时候,这种可变数据类型时会出现同时变动的情况。

count

解释内容

  • count(self, value, /)
    Return number of occurrences of value.

参数

  • self:默认参数,表示是当前从类中实例化出来的对象
  • value:统计的值
  • /:限制参数,表示在/之前的参数都是仅位置参数

作用

  • Return number of occurrences of value.
  • 返回值出现的次数
extend

解释内容

  • extend(self, iterable, /)
    Extend list by appending elements from the iterable.

参数

  • self:默认参数,表示是当前从类中实例化出来的对象
  • iterable(可迭代的):可迭代的对象
  • /:限制参数,表示在/之前的参数都是仅位置参数

作用

  • Extend list by appending elements from the iterable.
  • 通过可迭代对象中的元素来扩展list
index

解释内容

  • index(self, value, start=0, stop=9223372036854775807, /)
    Return first index of value.
  • Raises ValueError if the value is not present.

参数

  • self:默认参数,表示是当前从类中实例化出来的对象
  • value:需要查找的值
  • start=0:默认参数,指从0开始
  • stop=9223372036854775807:默认参数,指至9223372036854775807结束
  • /:限制参数,表示在/之前的参数都是仅位置参数

作用

  • 返回指定值第一次出现的索引值

注意:如果值不存在,则返回ValueError的错误

insert

解释内容

  • insert(self, index, object, /)
    nsert object before index.

参数

  • self:默认参数,表示是当前从类中实例化出来的对象
  • index:插入的位置索引
  • object:插入的元素
  • /:限制参数,表示在/之前的参数都是仅位置参数

作用

  • 在索引之前插入对象
pop

解释内容

  • pop(self, index=-1, /)
    Remove and return item at index (default last).
    Raises IndexError if list is empty or index is out of range.

参数

  • self:默认参数,表示是当前从类中实例化出来的对象
  • index=-1:默认参数,默认操作最后一个元素
  • /:限制参数,表示在/之前的参数都是仅位置参数

作用

  • 移除并返回指定索引的元素,如为指定索引则默认为最后一个元素

注意:如果为空列表或者索引超过范围,则会报IndexError错误

remove

解释内容

  • remove(self, value, /)
    Remove first occurrence of value.
    Raises ValueError if the value is not present.

参数

  • self:默认参数,表示是当前从类中实例化出来的对象
  • value:要移除的值
  • /:限制参数,表示在/之前的参数都是仅位置参数

作用

  • 移除第一次出现的值

注意:如果值不存在,则会报ValueError错误

reverse

解释内容

  • reverse(self, /)
    Reverse IN PLACE.

参数

  • self:默认参数,表示是当前从类中实例化出来的对象
  • /:限制参数,表示在/之前的参数都是仅位置参数

作用

  • IN PLACE(在原地)
  • 在原地反转 – 倒序
sort

解释内容

  • sort(self, /, *, key=None, reverse=False)
    Sort the list in ascending order and return None.

参数

  • self:默认参数,表示是当前从类中实例化出来的对象
  • /:限制参数,表示在/之前的参数都是仅位置参数
  • *:限制参数,表示在 * 之后的参数都是关键字参数
  • key=None:默认返回None
  • reverse=False:默认不反转顺序

作用

  • 用来给容器排序
  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值