列表list

时间:2023-03-09 09:06:13
列表list

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

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

>>> lia = [2]
>>> print lia[0]
2

Built-in Methods

list.append()

Appends a passed obj into the existing list.

Syntax&P

list.append(obj)
#obj -- This is the object to be appended in the list.

eg

>>> lia.append('A')
>>> lia.append(4)
>>> lia.append([23,5,'12'])
>>> print lia
[2, 'A', 4, [23, 5, '12']]  #将一个集合插入到lia的结尾

list.extend()

Appends the contents of seq to list.return none.

Syntax&P

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

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

Returns count of how many times obj occurs in list.

Syntax&P

list.count(obj)
#obj -- This is the object to be counted in the 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()

Remove the specified object in list.

Syntax&P

list.remove(obj)
#obj -- This is the object to be removed 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()

Returns the lowest index in list that obj appears.

Syntax&P

list.index(obj)
#obj -- This is the object to be find out.

eg

>>> lia.index('xx')
5

list.pop()

Removes and returns last object or obj from the list and returns the removed object from the list.

Syntax&P


list.pop(obj=list[-1])
#obj -- This is an optional parameter, index of the object to be removed 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()

Reverses objects of list in place.

Syntax&P

list.reverse()

eg

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

list.sort()

Sorts objects of list, use compare func if given.

Syntax&P

list.sort([func])

eg

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

Built-in Functions

cmp()

Compares elements of two lists.

If elements are of the same type, perform the compare and return the result. If elements are different types, check to see if they are numbers.

  • If numbers, perform numeric coercion if necessary and compare.
  • If either element is a number, then the other element is "larger" (numbers are "smallest").
  • Otherwise, types are sorted alphabetically by name.

If we reached the end of one of the lists, the longer list is "larger." If we exhaust both lists and share the same data, the result is a tie, meaning that 0 is returned.

Syntax&P

cmp(list1, list2)

eg

>>> print lia,lib
[2, 'A', 'Another list'] ['Another list']
>>> cmp(lia,lib)
-1
>>> cmp(lib,lia)
1
>>> cmp(lib,lib)
0

len()

The method len() returns the number of elements in the list.

Syntax&P

len(list)
#list -- This is a list for which number of elements to be counted.

eg

>>> print lia
[2, 'A', 'Another list']
>>> len(lia)
3

max()

Returns the elements from the list with maximum value.

Syntax&P

max(list)
#list -- This is a list from which max valued element to be returned.

eg

>>> max(lia)
'Another list'

min()

Returns the elements from the list with minimum value.

Syntax&P

min(list)
#list -- This is a list from which min valued element to be returned.

eq

>>> min(lia)
2

list()

Takes sequence types and converts them to lists. This is used to convert a given tuple into list.

Note: Tuple are very similar to lists with only difference that element values of a tuple can not be changed and tuple elements are put between parentheses instead of square bracket.

Syntax&P

list(seq)
#seq -- This is a tuple to be converted into list.

eg

>>> tua = {1,5,'tu'}
>>> litua = list(tua)
>>> print litua
[1, 'tu', 5]