Python的第八天
一、列表
1、切片
names= ["alex","li","liang","jack","black girl"]
>>> names[0:3]-----------顾头不顾尾
['alex', 'li', 'liang']
>>> names[:3]
['alex', 'li', 'liang']
2、倒着切
>>> names[3:]
['jack', 'black girl']
>>> names[-2:-1]
['jack']
>>> names[-2:]
['jack', 'black girl']
3、步长
>>> names[0::1]------------默认步长是1,省略第二个数表示取整个列表
['alex', 'li', 'liang', 'jack', 'black girl']
>>> names[0::2]
['alex', 'liang', 'black girl']
4、排序
>>> names.sort()
>>> names
['Wang', 'alex', 'black girl', 'jack', 'li', 'liang']-------------大写字母-小写字母-汉字
5、反转
>>> names.reverse()
>>> names
['liang', 'li', 'jack', 'black girl', 'alex', 'Wang']
>>> n = [1,2,3,4,5,6]
>>> n.reverse()
[1, 2, 3, 4, 5, 6]
>>> n.reverse()
[6, 5, 4, 3, 2, 1]
6、循环列表
>>> names
['alex', 'li', 'liang', 'wang', 'black girl']
>>> for i in names:
... print(i)
...
alex
li
liang
wang
black girl
>>> for i in enumerate(names):------------------打印索引
... print(i)
...
(0, 'alex')
(1, 'li')
(2, 'liang')
(3, 'wang')
(4, 'black girl')
二、dict 定义
info = {[key1:value1],[key2:value2]}
特性:1、key-value结构
2、key必须是不可变数据类型(字符串、数字)、必须唯一
3、可存放任意多个value、可修改、可以不唯一
4、无序
5、查询速度快
for example:
dict = {
"alex" : [27,40000],
"jack" : [28,6000],
"liang" : [20,600]
}
print("alex" in dict)
print(dict["alex"])