标签:
列表也是序列式的数据类型,可通过下标或切片操作来访问某一个或某一段连续的元素。
一、创建列表
列表可以由方括号‘[]‘来定义,通过赋予变量来创建。
>>> l1 = [‘abc‘,‘456‘,‘xyz‘] >>> l1 [‘abc‘, ‘456‘, ‘xyz‘]
列表中可以再嵌套一个列表,像这样:
>>> l2 = [‘abc‘,‘456‘,‘xyz‘,[‘789‘,‘lmn‘]] >>> l2 [‘abc‘, ‘456‘, ‘xyz‘, [‘789‘, ‘lmn‘]]
二、列表的操作
a.访问
采用切片操作与索引值方式来访问列表。
>>> l2 [‘abc‘, ‘456‘, ‘xyz‘, [‘789‘, ‘lmn‘]] >>> l2[0] ‘abc‘ >>> l2[-1] [‘789‘, ‘lmn‘] >>> l2[3][1] ‘lmn‘
b.更新
使用append()方法来将追加元素到列表中。
>>> l1 = [‘abc‘,‘456‘,‘xyz‘] >>> l1.append(‘Li‘) >>> l1 [‘abc‘, ‘456‘, ‘xyz‘, ‘Li‘]
三、几个有用的函数
1. len()
统计列表中元素的个数
>>> l3 [‘2‘, ‘3‘, ‘5‘, ‘6‘, ‘9‘] >>> len(l3) 5
2. max()&min()
获取列表中最大与最小值
>>> max(l3) ‘9‘ >>> min(l3) ‘2‘
3. enumerate()
遍历序列中的元素以及它们的下标
>>> for i,j in enumerate([1,2,3]): >>> print i,j 0 1 1 2 2 3
4. sum
将列表中的值相加
>>> l4 = [int(‘2‘),int(‘4‘)] >>> sum(l4) 6
注:只能对整数和字符串类型进行相加,否则会出错
四、内置函数
def append(self, p_object): #向列表中添加一个对象 def clear(self): #清空列表 def copy(self): #浅copy def count(self, value): #统计一个对象在列表中出现的次数 def extend(self, iterable): #将内容添加到列表中 >>> l1 [‘abc‘, ‘456‘, ‘xyz‘, ‘Li‘] >>> l1 [‘abc‘, ‘456‘, ‘xyz‘, ‘Li‘] >>> l2 [‘abc‘, ‘456‘, ‘xyz‘, [‘789‘, ‘lmn‘]] >>> l1.extend(l2) >>> l1 [‘abc‘, ‘456‘, ‘xyz‘, ‘Li‘, ‘abc‘, ‘456‘, ‘xyz‘, [‘789‘, ‘lmn‘]] def index(self, value, start=None, stop=None): #获取列表中的索引位置 """ def insert(self, index, p_object): #指定下标插入元素 def pop(self, index=None): #指定下标删除元素,并获取删除元素(如果没有指定对象,默认是最后一个值) def remove(self, value): #指定值删除的元素 def reverse(self): #反向列表中元素 def sort(self, key=None, reverse=False): #对列表中的元素进行排序 >>> l3 = [‘3‘,‘6‘,‘5‘,‘9‘,‘2‘] >>> l3.sort() >>> l3 [‘2‘, ‘3‘, ‘5‘, ‘6‘, ‘9‘] def __add__(self, *args, **kwargs): # real signature unknown """ Return self+value. """ pass def __contains__(self, *args, **kwargs): # real signature unknown """ Return key in self. """ pass def __delitem__(self, *args, **kwargs): # real signature unknown """ Delete self[key]. """ pass def __eq__(self, *args, **kwargs): # real signature unknown """ Return self==value. """ pass def __getattribute__(self, *args, **kwargs): # real signature unknown """ Return getattr(self, name). """ pass def __getitem__(self, y): # real signature unknown; restored from __doc__ """ x.__getitem__(y) <==> x[y] """ pass def __ge__(self, *args, **kwargs): # real signature unknown """ Return self>=value. """ pass def __gt__(self, *args, **kwargs): # real signature unknown """ Return self>value. """ pass def __iadd__(self, *args, **kwargs): # real signature unknown """ Implement self+=value. """ pass def __imul__(self, *args, **kwargs): # real signature unknown """ Implement self*=value. """ pass def __init__(self, seq=()): # known special case of list.__init__ """ list() -> new empty list list(iterable) -> new list initialized from iterable‘s items # (copied from class doc) """ pass def __iter__(self, *args, **kwargs): # real signature unknown """ Implement iter(self). """ pass def __len__(self, *args, **kwargs): # real signature unknown """ Return len(self). """ pass def __le__(self, *args, **kwargs): # real signature unknown """ Return self<=value. """ pass def __lt__(self, *args, **kwargs): # real signature unknown """ Return self<value. """ pass def __mul__(self, *args, **kwargs): # real signature unknown """ Return self*value.n """ pass @staticmethod # known case of __new__ def __new__(*args, **kwargs): # real signature unknown """ Create and return a new object. See help(type) for accurate signature. """ pass def __ne__(self, *args, **kwargs): # real signature unknown """ Return self!=value. """ pass def __repr__(self, *args, **kwargs): # real signature unknown """ Return repr(self). """ pass def __reversed__(self): # real signature unknown; restored from __doc__ """ L.__reversed__() -- return a reverse iterator over the list """ pass def __rmul__(self, *args, **kwargs): # real signature unknown """ Return self*value. """ pass def __setitem__(self, *args, **kwargs): # real signature unknown """ Set self[key] to value. """ pass def __sizeof__(self): # real signature unknown; restored from __doc__ """ L.__sizeof__() -- size of L in memory, in bytes """ pass __hash__ = None
标签:
原文地址:http://www.cnblogs.com/jef1025/p/5162736.html