15468 发表于 2018-8-6 11:03:43

python---list()用法

  list列表
  以后再继续完善
  help(list) 使用help查看list

  Help on>  class list(object)
  |list() -> new empty list空列表
  |list(iterable) -> new list initialized from iterable's items
  |
  |Methods defined here:各种方法的使用
  |1.__add__(...)列表相加,相当于连接
  |      x.__add__(y) <==> x+y
  |例:方法1:

  方法2:(两种方法结果一样,对于后面的介绍,只对一种举例介绍)

  |2.__contains__(...)包含关系
  |      x.__contains__(y) <==> y in x如果y在列表x中,返回Ture,否则False
  |
  |3.__delitem__(...)
  |      x.__delitem__(y) <==> del x删除列表中的一个元素,注意y是索引
  |
  |4.__delslice__(...)删除列表中的连续几个元素
  |      x.__delslice__(i, j) <==> del x
  |
  |      Use of negative indices is not supported.!!不支持负数索引,如下:如果负数索引有效,则删除导数第一个和导数第二个元素,但是此处没有删除任何元素。
  |
  |5.__eq__(...)
  |      x.__eq__(y) <==> x==y列表之间的比较:只比较第一个元素
  |
  |__ge__(...)略
  |      x.__ge__(y) <==> x>=y
  |
  |6.__getattribute__(...)
  |      x.__getattribute__('name') <==> x.name
  |
  |7.__getitem__(...)
  |      x.__getitem__(y) <==> x
  |
  |8.__getslice__(...)
  |      x.__getslice__(i, j) <==> x
  |
  |      Use of negative indices is not supported.不支持负数索引
  |
  |9.__gt__(...)
  |      x.__gt__(y) <==> x>y
  |
  |10.__iadd__(...)
  |      x.__iadd__(y) <==> x+=y把x+y的结果赋给x
  |
  |11.__imul__(...)
  |      x.__imul__(y) <==> x*=y注意这里*2是将列表复制2次,不是对里面的数值进行计算
  |
  |12.__init__(...)
  |      x.__init__(...) initializes x; see help(type(x)) for signature
  |
  |13.__iter__(...)
  |      x.__iter__() <==> iter(x)
  |
  |14.__le__(...)
  |      x.__le__(y) <==> x<=y
  |
  |15.__len__(...)
  |      x.__len__() <==> len(x) 列表的长度
  |
  |16.__lt__(...)
  |      x.__lt__(y) <==> x<y
  |
  |17.__mul__(...)
  |      x.__mul__(n) <==> x*n 这里没有赋值,所以,只是暂时把list1复制,但是list1本身没有变化
  |
  |18.__ne__(...)
  |      x.__ne__(y) <==> x!=y
  |
  |19.__repr__(...)
  |      x.__repr__() <==> repr(x) 与str()
  |
  |20.__reversed__(...)
  |      L.__reversed__() -- return a reverse iterator over the list
  |
  |21.__rmul__(...)
  |      x.__rmul__(n) <==> n*x
  |
  |22.__setitem__(...)
  |      x.__setitem__(i, y) <==> x=y
  |
  |23.__setslice__(...)
  |      x.__setslice__(i, j, y) <==> x=y
  |
  |      Useof negative indices is not supported.
  |
  |24.__sizeof__(...)

  |      L.__sizeof__() -->  |
  |25.append(...)追加元素,追加在原列表的最后;
  |      L.append(object) -- append object to end
  |
  也可以另一个列表到原有列表中

  |26.count(...)计算某个元素在列表中出现的次数
  |      L.count(value) -> integer -- return number of occurrences of value
  |
  |27.extend(...)可以将列表作为参数,并把另一个列表中的左右元素添加到另一个列表的尾部
  |      L.extend(iterable) -- extend list by appending elements from the iterable
  |
  |28.index(...)
  |      L.index(value, ]) -> integer -- return first index of value.
  |      Raises ValueError if the value is not present.
  |
  |29.insert(...)插入元素,需要指定插入位置的索引
  |      L.insert(index, object) -- insert object before index
  |
  |30.pop(...)删除元素,如果不指定位置,则默认删除最后一个元素
  |      L.pop() -> item -- remove and return item at index (default last).
  |      Raises IndexError if list is empty or index is out of range.
  |如果列表为空,或者索引超出实际范围则报错

  如果列表为空,或者索引超出实际范围则报错

  |31.remove(...)删除某个元素第一次出现的位置(只知道删除的元素,并不知道该元素的索引)
  |      L.remove(value) -- remove first occurrence of value.
  |      Raises ValueError if the value is not present.
  |
  |32.reverse(...)将列表反向排序
  |      L.reverse() -- reverse *IN PLACE*
  |
  |33.sort(...)排序:从低到高(列表中的元素属于同一类型???)
  |      L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;
  |      cmp(x, y) -> -1, 0, 1
  |
  |----------------------------------------------------------------------
  |Data and other attributes defined here:
  |
  |34.__hash__ = None
  |
  |35.__new__ = <built-in method __new__ of type object>
  |      T.__new__(S, ...) -> a new object with type S, a subtype of T
页: [1]
查看完整版本: python---list()用法