python基础之列表、元组
一、列表
列表(list)是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储、修改等操作
作用:多个装备,多个爱好,多门课程,多个女朋友等
定义:[ ]内可以有多个任意类型的值,逗号分隔
定义列表:name = [ ] #name等于中括号就是定义列表
列表用来存储数据的
1、基本操作
列表的添加 |
append |
追加,在列表的尾部加入指定的元素 |
insert |
将指定的元素插入到对应的索引位上,注意负索引 |
|
列表的删除 |
pop |
弹出,返回并删除指定索引位上的数据,默认删除索引为-1的数据(相当于剪切) |
remove |
从左往右删除一个指定的元素 |
|
del |
删除整个列表或列表的数据,del是python内置功能,不是列表独有的 |
|
列表的查找 注:列表没有find方法 |
count |
计数,返回要计数的元素在列表当中的个数 |
index |
查找,从左往右返回查找到的第一个指定元素的索引,如果没有找到,报错 |
|
列表的排序 |
reverse |
顺序倒序 |
sort |
按照ascii码表顺序进行排序 |
2、定义列表
name = ['tom','jack','ren','yan'] print(type(name)) ------------------------------------------------- <class 'list'>
3、遍历列表
name = ['tom','jack','ren','yan'] for i in name: print(i) ------------------------------------- tom jack ren yan --------------------------------------------------- #通过下标访问 print(name[0]) print(name[3]) -------------------------------- tom yan
4、切片:取多个元素
name = ['tom','jack','ren','yan','zhong','qq','weixin'] print(name[0:3]) #取下标0至3之间的元素,包含0,不包含3 print(name[:3]) print(name[:-1]) #取下标0至最后一个的元素,包含0,不包含-1 print(name[4:]) print(name[::2]) #2表示每隔一个元素,就取一个 print(name[0:4:2]) #在0到4之间每隔一个元素取一个 ------------------------------------------------------------ ['tom', 'jack', 'ren'] ['tom', 'jack', 'ren'] ['tom', 'jack', 'ren', 'yan', 'zhong', 'qq'] ['zhong', 'qq', 'weixin'] ['tom', 'ren', 'zhong', 'weixin'] ['tom', 'ren']
5、追加
name = ['tom','jack','ren','yan'] print(name) name.append('我是新来的') #在列表的尾部加入指定的元素 print(name) ------------------------------------------------------------- ['tom', 'jack', 'ren', 'yan'] ['tom', 'jack', 'ren', 'yan', '我是新来的']
6、插入
name = ['tom','jack','ren','yan'] print(name) name.insert(1,'插队的') #插入到索引1位置 print(name) --------------------------------------------- ['tom', 'jack', 'ren', 'yan'] ['tom', '插队的', 'jack', 'ren', 'yan']
7、修改
name = ['tom','jack','ren','yan'] print(name) name[1] = '该换人了' print(name) ------------------------------------------------- ['tom', 'jack', 'ren', 'yan'] ['tom', '该换人了', 'ren', 'yan']
8、删除
name = ['tom','jack','ren','yan','zhong','hello','go','python'] del name[0] #删除索引为0的指定元素 print(name) #tom已删除 print(name.pop(0)) #输出要剪切的指定元素(知道索引,不知道内容) print(name) #Jack已删除 print(name.pop()) #pop默认删除最后一个元素 print(name) print(name.remove('ren')) #删除指定元素,且不输出(知道内容不知道索引) print(name) --------------------------------------------------------- ['jack', 'ren', 'yan', 'zhong', 'hello', 'go', 'python'] jack ['ren', 'yan', 'zhong', 'hello', 'go', 'python'] python ['ren', 'yan', 'zhong', 'hello', 'go'] None ['yan', 'zhong', 'hello', 'go']
9、扩展
name = ['tom','jack','ren','yan','zhong'] languages = ['pyhon','go','C#','PHP'] name.extend(languages) print(name) -------------------------------------------- ['tom', 'jack', 'ren', 'yan', 'zhong', 'pyhon', 'go', 'C#', 'PHP']
10、拷贝
name = ['tom','jack','ren','yan','zhong'] name_copy = name.copy() print(name_copy) -------------------------------------------------- ['tom', 'jack', 'ren', 'yan', 'zhong']
11、统计
name = ['tom','ren','ren','yan','zhong'] print(name.count('ren')) ---------------------------------------------------- 2
12、排序&翻转
name = ['tom','ren','ren','yan','zhong','alex',1,12,2,3,23] name.sort() #不同字符类型不能放在一起排序 print(name) ------------------------------------- Traceback (most recent call last): File "C:/Users/renyz02/Desktop/py_test1/20190911/list.py", line 76, in <module> name.sort() TypeError: '<' not supported between instances of 'int' and 'str'
name = ['tom','ren','ren','yan','zhong','alex'] print(name) name.sort() #排序(字母按照ASCII码,数字从小到大) print(name) name.reverse() #反转原来的顺序(数字、字母同理) print(name) ----------------------------------------------------- ['tom', 'ren', 'ren', 'yan', 'zhong', 'alex'] ['alex', 'ren', 'ren', 'tom', 'yan', 'zhong'] ['zhong', 'yan', 'tom', 'ren', 'ren', 'alex'] ---------------------------------------------------------------- name = ['tom','ren','ren','yan','zhong','alex'] print(name) # name.sort() # print(name) name.reverse() print(name) -------------------------------------------------- ['tom', 'ren', 'ren', 'yan', 'zhong', 'alex'] ['alex', 'zhong', 'yan', 'ren', 'ren', 'tom']
13、获取下标
name = ['tom','ren','ren','yan','zhong','alex'] print(name.index('yan')) --------------------------------------------------- 3
二、元组
另一种有序列表叫元组:tuple。tuple和list非常类似,但是tuple一旦初始化就不能修改
元组特点:是不可变
定义:与列表类型比,只不过[ ]换成() ,如:age=(11,22,33,44,55)
作用:存多个值,对比列表来说,元组不可变(是可以当做字典的key的)
元组的查找 |
index |
从左往右返回第一个遇到的指定元素的索引,如果没有,报错 |
count |
返回元组当中指定元素的个数 |
1、创建元组
num = (11,22,33,44) print(type(num)) print(num) ---------------------------------------------- <class 'tuple'> (11, 22, 33, 44)
定义一个空的tuple,可以写成():
num2 = () print(type(num2)) -------------------------------------------- <class 'tuple'>
但是,如果定义一个只有一个元素的tuple,需要加,:
num3 = (22) print(type(num3)) num4 = (22,) print(type(num4)) ----------------------------------------------------------- <class 'int'> <class 'tuple'>
2、访问元组
num = (11,22,33,44) print(num) print(num[2]) ----------------------------------------- (11, 22, 33, 44) 33
num = (11,22,33,44) print(num) print(num.index(44)) #取索引 print(num.count(22)) #计数 ------------------------------------------------- (11, 22, 33, 44) 3 1
3、修改元组
说明:Python中不允许修改元组的数据,包括不能删除其中的元素。
元组是不可变的,也就是说,元组中的元素在被赋值后不能改变。但是,如果元素本身是一个可变数据类型的列表,那么其嵌套项可以被改变。
aa = (1,2,[11,22,33]) print(id(2)) #查看id aa[2][0] = 44 print(aa) print(id(2)) #id是一致的 -------------------------------------------------------- 140730237805232 (1, 2, [44, 22, 33]) 140730237805232
4、多维元祖访问
person = (("刘备","关羽","张飞"),("曹操","典韦"),("孙权","周瑜")) print(person[2]) print(person[0][0]) ----------------------------------------------------- ('孙权', '周瑜') 刘备