Python-List详解


列表(list)作为python的一种数据类型,常用它进行一些复合数据的分组,list的形式是[value1,value2,value3,value4....valuen],list的每项数据不需要是同一类型,可以是任意的python数据类型。

>>> l1 = [1,2,3,'name',[1,2,3],{'age':18},(1,2,3)]

>>> l1

[1, 2, 3, 'name', [1, 2, 3], {'age': 18}, (1, 2, 3)]


list列表中的每个元素是根据索引来进行位置标识,可以通过指定索引范围的方式来得到想要的数据。

索引的第一位从0开始,最后一位可以用-1表示,倒数第二位用-2表示,如此类推。


#可以使用lst[index]的方式获得指定索引元素的值。

>>> l2

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> l2[5]

5

>>> l2[1]

1

>>> l2[-1]

9

>>> l2[-3]

7


list列表的切片操作:

切片操作是python中对有序的数据类型最常见的一种操作,通过元素索引获得索引范围内的一段数据。

常用格式为:

lst[startindex:endindex:step]

起始索引:结束索引:步长,步长为正数时,表示index+step,索引为负数时为index-step。

步长为可选选项,不设置默认步长为1


#python中的索引切片操作都是(n:m]的规则,即包含n不包含m,这点需要注意。

>>> l2

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

l2[0:5]表示,从索引0开始取,直到索引5为止,但不包括索引5。即索引0,1,2,3,4,正好是5个元素。

>>> l2[0:5]

[0, 1, 2, 3, 4]


#步长为2,每2位取一位数据。

>>> l2[0:5:2]

[0, 2, 4]


#通过切片反转列表

>>> l5

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

>>> l5[::-1]

[10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]



#[:]这种形式下,startindex不指定默认为的第一个索引(0),endindex不指定,表示取startindex到列表的最后一个元素。

>>> l2[:]

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

#列表的复制

>>> a = l2[:]

>>> a

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> id(a)

139895940789768

>>> id(l2)

139896080732808


>>> l3 = l1[3:]+l2[:5]

>>> l3

['name', [1, 2, 3], {'age': 18}, (1, 2, 3), 0, 1, 2, 3, 4]

>>> 


#列表为可变序列,列表中的元素都是可变的,可以使用lst[index]=newvalue的方式来改变lst中元素的值

>>> l3

['name', [1, 2, 3], {'age': 18}, (1, 2, 3), 0, 1, 2, 3, 4]

>>> l3[1] = 'age'

>>> l3

['name', 'age', {'age': 18}, (1, 2, 3), 0, 1, 2, 3, 4]

>>> 


#使用列表切片复制,可以改变列表中一段的元素,或者完全能清空列表。

>>> l3

['name', 'age', {'age': 18}, (1, 2, 3), 0, 1, 2, 3, 4]

>>> l3[2:4]=['newvalue1','newvalue2']

>>> l3

['name', 'age', 'newvalue1', 'newvalue2', 0, 1, 2, 3, 4]

>>> l3[-2:-1]

[3]


#切片赋值不需要索引个数等于newvalue的个数。

>>> l3[-2:-1]=['test1','test2','test3']

>>> l3

['name', 'age', 'newvalue1', 'newvalue2', 0, 1, 2, 'test1', 'test2', 'test3', 4]


#在指定索引位置插入一些数据。

>>> l4 = [1,2,3]

>>> l4[1:1] = ['new values',' new test',(1,2,3)]

>>> l4

[1, 'new values', ' new test', (1, 2, 3), 2, 3]


#在开始位置插入自己本身。

>>> l4[:0] = l4

>>> l4

[1, 'new values', ' new test', (1, 2, 3), 2, 3, 1, 'new values', ' new test', (1, 2, 3), 2, 3]


#清空列表

>>> l4[:] = []

>>> l4

[]


可以使用内建函数len查询list的长度。

>>> l5

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

>>> len(l5)

11

>>>


列表的方法

列表是可变的数据类型,python为其提供了一些方法,来对列表进行处理。

#为列表增加元素。

 |  append(...)

 |      L.append(object) -> None -- append object to end

 在列表结尾追加元素,没有返回值。


>>> l1 = [1,2,3,4,5]

>>> l1.append(6)

>>> l1

[1, 2, 3, 4, 5, 6]


#清空列表,等效于l1[:]=[]。

 |  clear(...)

 |      L.clear() -> None -- remove all items from L

 清空猎的所有元素,没有返回值,

>>> l1

[1, 2, 3, 4, 5, 6]

>>> l1.clear()

>>> l1

[]

>>>


#列表拷贝,等效于l2=l1[:]

 |  copy(...)

 |      L.copy() -> list -- a shallow copy of L

 列表的一个浅拷贝,返回一个新的list对象,拷贝生成的新对象,是在内存在重新定义的,跟原list没关联,所以浅拷贝和l2=l1这种赋值的形式是不同的。

>>> l2 = l1.copy()

>>> l2

[1, 2, 3, 4]

>>> l1

[1, 2, 3, 4]

>>> id(l1)

139895940823496

>>> id(l2)

139895940874312


#列表元素计数

 |  count(...)

 |      L.count(value) -> integer -- return number of occurrences of value

 统计value在列表中出现的次数,返回次数的数值(integer)。

>>> l3

[1, 2, 2, 2, 2, 2, 3, 4, 5, 6, 7, 8, 9]

>>> l3.count(2)

5


#列表追加可迭代对象。

 |  extend(...)

 |      L.extend(iterable) -> None -- extend list by appending elements from the iterable

 用可迭代对象的元素来追加到列表,无返回值,这类可迭代对象可是list、str、dict等


>>> l1

[1, 2, 3, 4]

>>> t1=(1,2,3)

>>> l1.extend(t1)

>>> l1

[1, 2, 3, 4, 1, 2, 3]

#可迭代对象为字典时,只会使用字典的key进行迭代添加。

>>> d1={'name':'zj','age':18}

>>> l1.extend(d1)

>>> l1

[1, 2, 3, 4, 1, 2, 3, 'name', 'age']

>>> s1="python"

>>> l1.extend(s1)

>>> l1

[1, 2, 3, 4, 1, 2, 3, 'name', 'age', 'p', 'y', 't', 'h', 'o', 'n']

>>> 



#查找list中指定元素的索引

 |  index(...)

 |      L.index(value, [start, [stop]]) -> integer -- return first index of value.

 |      Raises ValueError if the value is not present.

 查找元素索引第一次出现的位置,返回索引的数字,如果指定的元素不存在则抛出一个ValueError的异常。

 [start,[stop]]为可选参数,表示在指定的索引范围内查找元素,start为开始索引,stop为结束索引,包含start,不包含stop。

>>> l1 = [1,2,3,4,5,6,7,2,8,9,10]

>>> l1.index(2)

1

>>> l1.index(2,3,10)

7

>>> 


#list插入元素,等效于lst[index:index]=[value]

 |  insert(...)

 |      L.insert(index, object) -- insert object before index

 在指定的索引位置前插入元素


>>> l5.insert(3,999)

>>> l5

[0, 1, 2, 999, 3, 4, 5, 6, 7, 8, 9, 10, 11]

>>> l5[3:3]=[999]

>>> l5

[0, 1, 2, 999, 999, 3, 4, 5, 6, 7, 8, 9, 10, 11]

>>> 


#删除列表指定位置的元素, 并返回它. 如果没有指定索引, a.pop 移除并返回列表的最后一个元素.

 |  pop(...)

 |      L.pop([index]) -> item -- remove and return item at index (default last).

 |      Raises IndexError if list is empty or index is out of range.

 删除并返回指定索引的元素(不指定index,默认删除返回最后一个元素)

 如果列表为空,或者超出索引范围,则抛出一个IndexError异常。

 (函式原型的 [index] 在中方括号中,意味着它是一个可选参数, 而不是你应当在那里键入一个方括号. 你将会在 Python 库参考中经常见到这种表示法.)

>>> l6=[1,2,3,4,5,6,7]

>>> l6.pop()

7

>>> l6.pop(3)

4

>>> l6.pop(8)

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

IndexError: pop index out of range

>>>


#删除指定的元素

 |  remove(...)

 |      L.remove(value) -> None -- remove first occurrence of value.

 |      Raises ValueError if the value is not present.

删除list中第一次出现的指定元素值,没有返回值,如果列表中有多个该元素,只会删除第一个。

如果指定删除的元素不存在,会抛出一个ValueError的异常

>>> l7=[1, 2, 3, 4, 5, 2, 2, 2, 6, 7, 8]

>>> l7.remove(2)

>>> l7

[1, 3, 4, 5, 2, 2, 2, 6, 7, 8]

>>> l7.remove(10)

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

ValueError: list.remove(x): x not in list

>>> 


#反转列表

 |  reverse(...)

 |      L.reverse() -- reverse *IN PLACE*

 就地反转列表,没有返回值。

>>> l8 = [1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> l8.reverse()

>>> l8

[9, 8, 7, 6, 5, 4, 3, 2, 1]


#列表排序

 |  sort(...)

 |      L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE*

 就地列表进行排序,没有返回值,

 key为指定排序方式,默认为空,reverse为是否对结果进行反转,默认为不反转

>>> l9=[2, 1, 3, 6, 5, 4, 9, 8, 7, 1]

>>> l9.sort()

>>> l9

[1, 1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> l9=[2, 1, 3, 6, 5, 4, 9, 8, 7, 1]

>>> l9.sort(re)

repr(      return     reversed(  

>>> l9.sort(reverse=True)

>>> l9

[9, 8, 7, 6, 5, 4, 3, 2, 1, 1]

>>>