Quantcast
Channel: CodeSection,代码区,Python开发技术文章_教程 - CodeSec
Viewing all articles
Browse latest Browse all 9596

列表

$
0
0

python是一种面向对象的语言,但它不像C++一样把标准类都封装到库中,而是进行了进一步的封装,语言本身就集成一些类和函数,比如print,list,dict etc. 给编程带来很大的便捷

Python中列表有点像C中的数组,list里面可以承载不同的数据类型,而且封装了常用的方法用于对list的操作,Python列表索引的下标默认是从第0个开始的,即 lia[0] .如果需要获取最后一个元素,可以写 lia[-1] 。 >>> lia = [2] >>> print lia[0] 2 方法 list.append() Description

The 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 Value

This 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() Description

The method extend() appends the contents of seq to list.

Syntax list.extend(seq) #seq -- This is the list of elements Return Value

This 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() Description

The 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 Value

This 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() Description

The 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 Value

This 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() Description

The method reverse() reverses objects of list in place.

Syntax list.reverse() Return Value

This 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() Description

The method sort() sorts objects of list, use compare func if given.

Syntax list.sort([func]) Return Value

This method does not return any value but reverse the given object from the list.

eg >>> lia.sort() >>> print lia [2, 'A', 'Another list']

Viewing all articles
Browse latest Browse all 9596

Trending Articles