码迷,mamicode.com
首页 > 编程语言 > 详细

python列表

时间:2017-03-02 16:22:05      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:赋值   **kwargs   rate   默认   indexer   index   ace   play   art   

1.列表的表达

[1,2,3,4]
[olive,123]
["python",]

创建列表:

s=[1,2,3]
res=list([1,"python"])

2.列表功能

技术分享
  1 class list(object):
  2     """
  3     list() -> new empty list
  4     list(iterable) -> new list initialized from iterables items
  5     """
  6     def append(self, p_object): # real signature unknown; restored from __doc__
  7         """ L.append(object) -> None -- append object to end """
  8         pass
  9 
 10     def clear(self): # real signature unknown; restored from __doc__
 11         """ L.clear() -> None -- remove all items from L """
 12         pass
 13 
 14     def copy(self): # real signature unknown; restored from __doc__
 15         """ L.copy() -> list -- a shallow copy of L """
 16         return []
 17 
 18     def count(self, value): # real signature unknown; restored from __doc__
 19         """ L.count(value) -> integer -- return number of occurrences of value """
 20         return 0
 21 
 22     def extend(self, iterable): # real signature unknown; restored from __doc__
 23         """ L.extend(iterable) -> None -- extend list by appending elements from the iterable """
 24         pass
 25 
 26     def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__
 27         """
 28         L.index(value, [start, [stop]]) -> integer -- return first index of value.
 29         Raises ValueError if the value is not present.
 30         """
 31         return 0
 32 
 33     def insert(self, index, p_object): # real signature unknown; restored from __doc__
 34         """ L.insert(index, object) -- insert object before index """
 35         pass
 36 
 37     def pop(self, index=None): # real signature unknown; restored from __doc__
 38         """
 39         L.pop([index]) -> item -- remove and return item at index (default last).
 40         Raises IndexError if list is empty or index is out of range.
 41         """
 42         pass
 43 
 44     def remove(self, value): # real signature unknown; restored from __doc__
 45         """
 46         L.remove(value) -> None -- remove first occurrence of value.
 47         Raises ValueError if the value is not present.
 48         """
 49         pass
 50 
 51     def reverse(self): # real signature unknown; restored from __doc__
 52         """ L.reverse() -- reverse *IN PLACE* """
 53         pass
 54 
 55     def sort(self, key=None, reverse=False): # real signature unknown; restored from __doc__
 56         """ L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE* """
 57         pass
 58 
 59     def __add__(self, *args, **kwargs): # real signature unknown
 60         """ Return self+value. """
 61         pass
 62 
 63     def __contains__(self, *args, **kwargs): # real signature unknown
 64         """ Return key in self. """
 65         pass
 66 
 67     def __delitem__(self, *args, **kwargs): # real signature unknown
 68         """ Delete self[key]. """
 69         pass
 70 
 71     def __eq__(self, *args, **kwargs): # real signature unknown
 72         """ Return self==value. """
 73         pass
 74 
 75     def __getattribute__(self, *args, **kwargs): # real signature unknown
 76         """ Return getattr(self, name). """
 77         pass
 78 
 79     def __getitem__(self, y): # real signature unknown; restored from __doc__
 80         """ x.__getitem__(y) <==> x[y] """
 81         pass
 82 
 83     def __ge__(self, *args, **kwargs): # real signature unknown
 84         """ Return self>=value. """
 85         pass
 86 
 87     def __gt__(self, *args, **kwargs): # real signature unknown
 88         """ Return self>value. """
 89         pass
 90 
 91     def __iadd__(self, *args, **kwargs): # real signature unknown
 92         """ Implement self+=value. """
 93         pass
 94 
 95     def __imul__(self, *args, **kwargs): # real signature unknown
 96         """ Implement self*=value. """
 97         pass
 98 
 99     def __init__(self, seq=()): # known special case of list.__init__
100         """
101         list() -> new empty list
102         list(iterable) -> new list initialized from iterables items
103         # (copied from class doc)
104         """
105         pass
106 
107     def __iter__(self, *args, **kwargs): # real signature unknown
108         """ Implement iter(self). """
109         pass
110 
111     def __len__(self, *args, **kwargs): # real signature unknown
112         """ Return len(self). """
113         pass
114 
115     def __le__(self, *args, **kwargs): # real signature unknown
116         """ Return self<=value. """
117         pass
118 
119     def __lt__(self, *args, **kwargs): # real signature unknown
120         """ Return self<value. """
121         pass
122 
123     def __mul__(self, *args, **kwargs): # real signature unknown
124         """ Return self*value.n """
125         pass
126 
127     @staticmethod # known case of __new__
128     def __new__(*args, **kwargs): # real signature unknown
129         """ Create and return a new object.  See help(type) for accurate signature. """
130         pass
131 
132     def __ne__(self, *args, **kwargs): # real signature unknown
133         """ Return self!=value. """
134         pass
135 
136     def __repr__(self, *args, **kwargs): # real signature unknown
137         """ Return repr(self). """
138         pass
139 
140     def __reversed__(self): # real signature unknown; restored from __doc__
141         """ L.__reversed__() -- return a reverse iterator over the list """
142         pass
143 
144     def __rmul__(self, *args, **kwargs): # real signature unknown
145         """ Return self*value. """
146         pass
147 
148     def __setitem__(self, *args, **kwargs): # real signature unknown
149         """ Set self[key] to value. """
150         pass
151 
152     def __sizeof__(self): # real signature unknown; restored from __doc__
153         """ L.__sizeof__() -- size of L in memory, in bytes """
154         pass
155 
156     __hash__ = None
list

查看列表的功能属性:

技术分享
1 a=[1,2]
2 dir(a)
3 
4 #解释器运行结果
5 [__add__, __class__, __contains__, __delattr__, __delitem__, __dir__, __doc__, __eq__, __format__, __
6 ge__, __getattribute__, __getitem__, __gt__, __hash__, __iadd__, __imul__, __init__, __iter__, __le__7 , __len__, __lt__, __mul__, __ne__, __new__, __reduce__, __reduce_ex__, __repr__, __reversed__, __rmu
8 l__, __setattr__, __setitem__, __sizeof__, __str__, __subclasshook__, append, clear, copy, count, ex
9 tend, index, insert, pop, remove, reverse, sort]
dir

3.部分功能介绍

1)append(self, p_object):

在原有列表最后位置上追加新元素到列表,不生成新的列表。

技术分享
1 a=[1,]
2 b=a.append(dog)
3 print(a,type(a))
4 print(b,type(b))
5 
6 #运行结果
7 [1, dog] <class list>
8 None <class NoneType>     #没有返回新列表
demo

2)clear(self):

清空列表里面的元素,不生成新列表。

技术分享
1 a=[1,2,3,4]
2 b=a.clear()
3 print(a)
4 print(b)
5 
6 #运行结果
7 []
8 None
demo

3)copy(self):

复制一份列表,相当于一次浅拷贝。

技术分享
 1 a=[1,(9,2),3]
 2 b=a.copy()
 3 print(a,id(a),id(a[1]))
 4 print(b,id(b),id(b[1]))
 5 
 6 #赋值
 7 c=[1,2,3,4]
 8 d=c
 9 print(c,id(c))
10 print(d,id(d))
11 
12 #浅拷贝
13 e=[1,2,(2,3)]
14 f=copy.copy(e)
15 print(e,id(e),id(e[1]))
16 print(f,id(f),id(f[1]))
17 
18 #运行结果
19 [1, (9, 2), 3] 5336240 4666792
20 [1, (9, 2), 3] 5335400 4666792
21 [1, 2, 3, 4] 5335640
22 [1, 2, 3, 4] 5335640
23 [1, 2, (2, 3)] 5336280 1678203168
24 [1, 2, (2, 3)] 4668472 1678203168
demo

4)count(self, value):

统计列表中value元素的数量,返回一个int值。

技术分享
1 a=[1,1,1,2,2,3]
2 b=a.count(1)
3 print(a)
4 print(b,type(b))
demo

5)extend(self, iterable):

把iterable中的每个元素扩展成列表的元素,iterable可以是字符串、列表、字典、元组。

技术分享
 1 a=[1,2,3]
 2 b=a.extend("abc")
 3 c=a.extend([11,22])
 4 d=a.extend({"dog":22})
 5 print(a)
 6 print(b)
 7 print(c)
 8 print(d)
 9 
10 #运行结果
11 [1, 2, 3, a, b, c, 11, 22, dog]
12 None
13 None
14 None
demo

6)index(self, value, start=None, stop=None):

索引,查找列表中value元素位置,start与stop参数是查找起始与结束位置,默认为None,返回int数值,如果查找中不包含这个元素,则返回ValueError: ‘f‘ is not in list报错。

技术分享
1 a=[1,2,3,4,5,6,7,a,b,c]
2 b=a.index(a)
3 print(a)
4 print(b)
5 
6 #运行结果
7 [1, 2, 3, 4, 5, 6, 7, a, b, c]
8 7
demo

7)insert(self, index, p_object):

在列表index索引位置插入元素p_object,当index大于列表包含的元素个数时,在最后位置插入元素。

技术分享
1 a=[1,2,3,4,5,6,7,a,b,c]
2 b=a.insert(1,sb)
3 print(a)
4 print(b)
5 
6 #运行结果
7 [1, sb, 2, 3, 4, 5, 6, 7, a, b, c]
8 None
demo

8)pop(self, index=None):

从列表中取出index位置的值,index默认为None,此时取出列表中最后一个值。

技术分享
1 a=[1,2,3,4,5,6,7,a,b,c]
2 b=a.pop(7)
3 print(a)
4 print(b,type(b))
5 
6 #运行结果
7 [1, 2, 3, 4, 5, 6, 7, b, c]
8 a <class str>
demo

9)remove(self, value):

移除列表中第一个出现的value元素,value元素不存在列表中时,则ValueError: list.remove(x): x not in list报错。

技术分享
1 a=[1,2,3,4,5,6,7,a,b,c]
2 b=a.remove(a)
3 print(a)
4 print(b)
5 
6 #运行结果
7 [1, 2, 3, 4, 5, 6, 7, b, c]
8 None
demo

10)reverse(self):

反转列表元素的位置。

技术分享
1 a=[1,2,3,4,5,6,7,a,b,c]
2 b=a.reverse()
3 print(a)
4 print(b)
5 
6 #运行结果
7 [c, b, a, 7, 6, 5, 4, 3, 2, 1]
8 None
demo

11)sort(self, key=None, reverse=False):

给列表中的元素排序,key是指定取待排序元素的哪一项进行排序,默认为None,reverse实现降序排序,需要提供一个布尔值,默认为False(升序排列)。【sorted是生成个副本进行排序】

技术分享
 1 a=[1,2,8,9,5,6,7]
 2 c=[2,7,3,9,1]
 3 a.sort()
 4 b=sorted(c)
 5 print(a)
 6 print(b)
 7 
 8 #运行结果
 9 [1, 2, 5, 6, 7, 8, 9]
10 [1, 2, 3, 7, 9]
demo

12)切片[start:stop:step]

从列表中取出一部分元素生成一个新列表,start与stop默认为None,step表示步长值,默认是一个接着一个切取,如果为2,则表示进行隔一取一操作。步长值为正时表示从左向右取,如果为负,则表示从右向左取。步长值不能为0。

技术分享
 1 a=[1,2,8,9,5,6,7]
 2 b=a[1:5:2]
 3 c=a[:]
 4 print(a)
 5 print(b)
 6 print(c)
 7 
 8 #运行结果
 9 [1, 2, 8, 9, 5, 6, 7]
10 [2, 9]
11 [1, 2, 8, 9, 5, 6, 7]
demo

13)__add__(self, *args, **kwargs):

列表添加一个新给出列表中的元素,生成一个新的列表。

技术分享
1 a=[1,2,8,9,5,6,7]
2 b=a.__add__([a,1])
3 print(a)
4 print(b)
5 
6 #运行结果
7 [1, 2, 8, 9, 5, 6, 7]
8 [1, 2, 8, 9, 5, 6, 7, a, 1]
demo

14)__contains__(self, *args, **kwargs):

判断列表中是否包含某个元素,返回布尔值。

技术分享
1 a=[1,2,8,9,5,6,7]
2 b=a.__contains__(2)
3 print(a)
4 print(b)
5 
6 #运行结果
7 [1, 2, 8, 9, 5, 6, 7]
8 True
demo

 

python列表

标签:赋值   **kwargs   rate   默认   indexer   index   ace   play   art   

原文地址:http://www.cnblogs.com/olivexiao/p/6432219.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!