python是一种面向对象的语言,但它不像C++一样把标准类都封装到库中,而是进行了进一步的封装,语言本身就集成一些类和函数,比如print,list,dict etc. 给编程带来很大的便捷
Python中列表有点像C中的数组,list里面可以承载不同的数据类型,而且封装了常用的方法用于对list的操作,Python列表索引的下标默认是从第0个开始的,即 lia[0] .如果需要获取最后一个元素,可以写 lia[-1] 。 >>> lia = [2] >>> print lia[0] 2 方法 list.append() DescriptionThe method append() appends a passed obj into the existing list.
Syntax list.append(obj) #obj -- This is the object to be appended in the list. Return ValueThis method does not return any value but updates existing list.
eg >>> lia.append('A') >>> lia.append(4) >>> lia.append([23,5,'12']) >>> print lia [2, 'A', 4, [23, 5, '12']] #将一个集合插入到lia的结尾 list.extend() DescriptionThe method extend() appends the contents of seq to list.
Syntax list.extend(seq) #seq -- This is the list of elements Return ValueThis method does not return any value but add the content to existing list.
eg >>> lib = ["Another list"] >>> lia.extend(lib) >>> print lia [2, 'A', 4, [23, 5, '12'], 'Another list'] list.insert() list.insert(index, obj) #index -- This is the Index where the object obj need to be inserted. #obj -- This is the Object to be inserted into the given list. eg >>> lia.insert(2,'xx') >>> print lia [2, 'A', 'xx', 4, [23, 5, '12'], 'Another list'] list.count() DescriptionThe method count() returns count of how many times obj occurs in list.
Syntax list.count(obj) #obj -- This is the object to be counted in the list.Return Value
This method returns count of how many times obj occurs in list.
eg >>> lia.append('xx') >>> print lia [2, 'A', 'xx', 4, [23, 5, '12'], 'Another list', 'xx'] >>> lia.append('xx') >>> print lia [2, 'A', 'xx', 4, [23, 5, '12'], 'Another list', 'xx', 'xx'] >>> lia.count('xx') 3 list.remove()Description
This method remove() remove the specified object in list.
Syntax list.remove(obj) #obj -- This is the object to be removed from the list. Return ValueThis method does not return any value but removes the given object from the list.
eg >>> lia.remove('xx') >>> print lia [2, 'A', 4, [23, 5, '12'], 'Another list', 'xx', 'xx'] >>> lia.remove('xx') >>> print lia [2, 'A', 4, [23, 5, '12'], 'Another list', 'xx'] list.index()Description
The method index() returns the lowest index in list that obj appears.
Syntax list.index(obj) #obj -- This is the object to be find out.Return Value
This method returns index of the found object otherwise raise an exception indicating that value does not find.
eg >>> lia.index('xx') 5 list.pop() DescriptionThe method pop() removes and returns last object or obj from the list.
Syntax list.pop(obj=list[-1]) #obj -- This is an optional parameter, index of the object to be removed from the list. Return ValueThis method returns the removed object from the list.
eg >>> lia.pop() 'xx' >>> print lia [2, 'A', 4, [23, 5, '12'], 'Another list'] >>> lia.pop(2) 4 >>> lia.pop(-2) [23, 5, '12'] list.reverse() DescriptionThe method reverse() reverses objects of list in place.
Syntax list.reverse() Return ValueThis method does not return any value but reverse the given object from the list.
eg >>> lia.reverse() >>> print lia ['Another list', 'A', 2] list.sort() DescriptionThe method sort() sorts objects of list, use compare func if given.
Syntax list.sort([func]) Return ValueThis method does not return any value but reverse the given object from the list.
eg >>> lia.sort() >>> print lia [2, 'A', 'Another list']