首页 > 编程语言 > 详细

python---list()用法

时间:2017-03-29 16:42:24      阅读:442      评论:0      收藏:0      [点我收藏+]

list列表

以后再继续完善技术分享

help(list) 使用help查看list

Help on class list in module __builtin__:


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]删除列表中的一个元素,注意y是索引

 |  技术分享

 |  4.__delslice__(...)删除列表中的连续几个元素

 |      x.__delslice__(i, j) <==> del x[i:j]

 |  技术分享

 |      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[y]

 |  

 |  8.__getslice__(...)

 |      x.__getslice__(i, j) <==> x[i:j]

 |      

 |      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[i]=y

 |  

 |  23.__setslice__(...)

 |      x.__setslice__(i, j, y) <==> x[i:j]=y

 |      

 |      Use  of negative indices is not supported.

 |  

 |  24.__sizeof__(...)

 |      L.__sizeof__() -- size of L in memory, in bytes

 |  

 |  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, [start, [stop]]) -> 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([index]) -> 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


本文出自 “安静的程序媛” 博客,请务必保留此出处http://emily18.blog.51cto.com/7121141/1911386

python---list()用法

原文:http://emily18.blog.51cto.com/7121141/1911386

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!