1 list列表
如何通过一个变量存储公司所有员工的名字?
2 列表的功能
3 列表的定义与创建
定义:[]内以逗号分隔,按照索引,存放各种数据类型,每个位置代表一个元素
# list创建1 (推荐) >>> names = [] >>> names [] # list创建2 >>> list() [] >>> names = list() >>> names [] # list创建3 >>> names = list(['alex',1]) >>> names ['alex', 1]
4 列表的特点与常用操作
特性:
1.可存放多个值
2.按照从左到右的顺序定义列表元素,下标从0开始顺序访问,有序
3.可修改指定索引位置对应的值,可变
5 查询
>>> names = ['alex','jack','shanshan',4,4,5,6,7,7,8,9,4,4] >>> names[0] # 通过索引取值 'alex' >>> names[-1] # 通过索引从list右边开始取值 >>> names[-2] >>> names[-3] >>> names.index(6) #返回制定元素的索引值,从左右查找,找到第一个匹配值 则返回 >>> names.index(4) >>> i = names.index(6) >>> names[i] >>> names[names.index(6)]
>>> dir(names) #list的所有方法 ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__' , '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__' , '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__' , '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_e x__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__s izeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'ex tend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'] >>> >>> names.count(4) #统计制定元素的个数 >>> names.count(6) >>> names.count(7)
6 切片
>>> names ['alex', 'jack', 'shanshan', 4, 4, 5, 6, 7, 7, 8, 9, 4, 4] >>> >>> names[0-2] # names[-1] >>> names[0-3] # names[-2] >>> names[0:3] ['alex', 'jack', 'shanshan'] >>>
切片:顾头不顾尾
>>> names ['alex', 'jack', 'shanshan', 4, 4, 5, 6, 7, 7, 8, 9, 4, 4] >>> names[3:0:-1] [4, 'shanshan', 'jack']
7 增加和修改
追加
插入
修改
批量修改
8 删除
pop删除最后一个元素
remove删除从左找到的第一个值
del 全局删除
9 循环
>>> names ['alex', 5, 6, 7, 8, 9, 4, 4] >>> >>> for k in names: ... print('loop',k) ... loop alex loop 5 loop 6 loop 7 loop 8 loop 9 loop 4 loop 4 >>>
- for循环有边界
- while循环可以死循环下去
10 range
现在已经学了列表了,那现在同学们来一起创建一个从1-100的列表。
等1分钟,你们都打算怎么创建啊?
是直接从1写到100还是用循环?如果从1写到100未免太傻气了,用循环好像还好一点儿。但是用循环写还不够简单,在python里有一个现成的方法,可以直接生成一个1-100的数。
>>> range(1,100) range(1, 100) >>> list(range(1,100)) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99] >>> list(range(10)) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> list(range(0,100,2)) [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98]
# python2 >>> range(0,10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> >>> for i in range(10): ... print(i) ... 1 3 5 7 9 >>> >>> type(range(0,10)) <type 'list'>
# python3 >>> range(0,10) range(0, 10) >>> >>> for i in range(10): ... print(i) ... 1 3 5 7 9 >>> range(0,10) range(0, 10) >>> >>> type(range(0,10)) <class 'range'>
11 排序:按ASCII码排序
list是有序的
反转
12 clear
>>> names.clear() >>> names []
13 copy
浅copy
>>> n1
['a', 'b', 'c', ['alex', 'jack']]
>>> n2 = n1.copy()
>>> n2
['a', 'b', 'c', ['alex', 'jack']]
>>> id(n1)
12269064
>>> id(n2)
12269704
>>> n1[-1][-1] = 'tom'
>>> n1
['a', 'b', 'c', ['alex', 'tom']]
>>> n2
['a', 'b', 'c', ['alex', 'tom']]
>>> id(n1[-1])
12269512
>>> id(n2[-1])
12269512
>>> import copy >>> n1 ['a', 'b', 'c', ['alex', 'tom']] >>> n3 = copy.copy(n1) >>> n3 ['a', 'b', 'c', ['alex', 'tom']] >>> n1[-1][-1] = '1111' >>> n1 ['a', 'b', 'c', ['alex', '1111']] >>> n3 ['a', 'b', 'c', ['alex', '1111']]
深copy
>>> n1 ['a', 'b', 'c', ['alex', '1111']] >>> n4 = copy.deepcopy(n1) >>> n4 ['a', 'b', 'c', ['alex', '1111']] >>> n1[-1][-1] = '222' >>> n1 ['a', 'b', 'c', ['alex', '222']] >>> n4 ['a', 'b', 'c', ['alex', '1111']] >>> id(n1[-1]) 12269512 >>> id(n4[-1]) 12269960
copy总结
浅copy:a,b,如果a被修改了,b也会被修改
深copy:a,b。如果a被修改了,b不会被修改
14 len,in,split和join
#长度 >>> len(l) #包含 >>> 'seven' in l True >>> 'yuan' in l False
#分割 >>> s = 'hello world' >>> s.split(' ') ['hello', 'world'] >>> s2= 'hello,world' >>> s2.split(',') #连接 >>> l = ['hi','eva'] >>> '!'.join(l) 'hi!eva'