一、列表的创建
1.有三种形式的列表:普通列表(数据一致),混合列表(各种类型数据),空列表,比如:
>>> list1=[1,2,3,4,5]
>>> list1
[1, 2, 3, 4, 5]
>>> list2=['winter','2',3,['q','w','e','r'],70]
>>> list2
['winter', '2', 3, ['q', 'w', 'e', 'r'], 70]
>>> list3=[]
>>> list3
[]
二、列表的常用函数
(一)添加元素到列表
1.append():在列表的末尾添加一个元素,只能添加一个。元素的类型为:列表,字典,元组,集合,字符串等。比如:
>>> list1=[1,2,3,4]
>>> list1
[1, 2, 3, 4]
>>> list1.append([5,6])
>>> list1
[1, 2, 3, 4, [5, 6]]
>>> list1.append({1:'a',2:'b'})
>>> list1
[1, 2, 3, 4, [5, 6], {1: 'a', 2: 'b'}]
>>> list1.append((1,2,3))
>>> list1
[1, 2, 3, 4, [5, 6], {1: 'a', 2: 'b'}, (1, 2, 3)]
>>> list1.append({'q','w','e'})
>>> list1
[1, 2, 3, 4, [5, 6], {1: 'a', 2: 'b'}, (1, 2, 3), {'e', 'w', 'q'}]
>>> list1.append('zxcvbn')
>>> list1
[1, 2, 3, 4, [5, 6], {1: 'a', 2: 'b'}, (1, 2, 3), {'e', 'w', 'q'}, 'zxcvbn']
2.extend():在列表末尾添加一个元素,这个元素必须是列表,可以将多个元素放在一个列表中,比如:
>>> list2=[1,2]
>>> list2
[1, 2]
>>> list2.extend([5,6,7,'qwe'])
>>> list2
[1, 2, 5, 6, 7, 'qwe']
3.insert(index, object):在列表的index位置插入一个元素object,比如:
>>> list3=[1,2,3,4,5]
>>> list3
[1, 2, 3, 4, 5]
>>> list3.insert(2,99)
>>> list3
[1, 2, 99, 3, 4, 5]
(二)从列表中删除元素
1.remove(value):将列表中出现的第一个值为value的元素删除,比如:
>>> list4=[1,2,1,3,1,4]
>>> list4
[1, 2, 1, 3, 1, 4]
>>> list4.remove(1)
>>> list4
[2, 1, 3, 1, 4]
2.pop():默认删除最后一个元素,返回值为该元素,也可以指定删除的位置,比如:
>>> list1=[1,2,3,4,5]
>>> list1
[1, 2, 3, 4, 5]
>>> a=list1.pop()
>>> a
5
>>> list1
[1, 2, 3, 4]
>>> list1=[1,2,3,4,5]
>>> list1
[1, 2, 3, 4, 5]
>>> list1.pop(1)
2
>>> list1
[1, 3, 4]
3.del 语句,注意!这个不是函数,有三种形式:
① del <列表变量>[索引序列号]
② del <列表变量>[索引起始位置 :索引结束位置]
③ del <列表变量>[索引起始位置 :索引结束位置:步长]
④del <列表变量>
>>> list1=[1,2,3,4,5]
>>> list1
[1, 2, 3, 4, 5]
>>> del list1[1]
>>> list1
[1, 3, 4, 5]
>>> list1=[1,2,3,4,5]
>>> list1
[1, 2, 3, 4, 5]
>>> del list1[:3]
>>> list1
[4, 5]
>>> list1=[1,2,3,4,5]
>>> list1
[1, 2, 3, 4, 5]
>>> del list1[:6:2]
>>> list1
[2, 4]
>>> list1=[1,2,3,4,5]
>>> list1
[1, 2, 3, 4, 5]
>>> del list1
>>> list1
Traceback (most recent call last):
File "<pyshell#57>", line 1, in <module>
list1
NameError: name 'list1' is not defined
(三)其他常用函数
1.list():将其它的组合数据类型转化为列表类型,或生成一个空列表,比如:
>>> a=(1,2,3,4,5)
>>> b=list(a)
>>> b
[1, 2, 3, 4, 5]
2.clear():删除列表中所有元素,比如:
>>> list1=[1,2,3,4,5]
>>> list1
[1, 2, 3, 4, 5]
>>> list1.clear()
>>> list1
[]
3.copy():生成一个新列表,复制原列表list1的所有元素,比如:
>>> list1=[1,2,3,4,5]
>>> list1
[1, 2, 3, 4, 5]
>>> list2=list1.copy ()
>>> list2
[1, 2, 3, 4, 5]
注意:copy只能复制一层,即浅层复制,复制最外层,若列表list1中嵌套列表list3,list3中元素修改,则也会影响复制的列表list2
4.count(value): 统计列表中value值出现的个数,比如:
>>> list1=[1,2,1,3,1,4]
>>> list1
[1, 2, 1, 3, 1, 4]
>>> list1.count(1)
3
5.index(value, start, stop): 查找列表中第一次出现value值的位置
- value —— 要查找的元素。
- star —— 索引的起始位置。
- stop —— 索引的结束位置。
- 范围:[star,stop)
>>> list1=[1,2,1,3,1,4]
>>> list1
[1, 2, 1, 3, 1, 4]
>>> list1.index(1)
0
6.reverse():使列表中的元素倒置,比如:
>>> list1=[1,2,3,4,5]
>>> list1
[1, 2, 3, 4, 5]
>>> list1.reverse()
>>> list1
[5, 4, 3, 2, 1]
7.sort():使列表中的元素从小到大排序,若想从大到小排序,则sort(reverse=True),比如:
>>> list1=[1,5,2,78,4,32]
>>> list1.sort()
>>> list1
[1, 2, 4, 5, 32, 78]
>>> list1.sort(reverse=True)
>>> list1
[78, 32, 5, 4, 2, 1]