- A+
数组
1.定义
1 2 3 |
list0=[] #空列表 list1=[1,2,3,4] list2=['a','b','c',1,2,3] |
2.新增
1 2 3 |
list1=["a","b",1,2,True] list1.append('d') #在末尾增加一个元素(一次只能添加一个 ) list1.insert(0,'e') #在制定的位置 添加元素 |
3.列表复制
1 2 3 4 5 6 7 |
list1=["a","b",1,2,True] print(list1) list2=list1 print(list2) list3=['你','好','python'] list1.extend(list3) print(list1) |
4.查看
1 2 3 4 5 6 7 8 |
list1=["a","b","c","d","e"] print(list1) print(list1[0]) #根据索引位置查看元素 print(list1[2:4])# 切片 print(list1[::-1]) #列表反转 print(list1.reverse()) #列表反转 for i in list1: print(i,end="") #遍历列表 |
5.常用函数
1 2 3 4 5 6 7 |
list1=["a","b","c","d","e",1,1,2,33,44,30,19] print(len(list1)) #判断长度 print(list1.count('a')) #统计次数 print(list1.index(1)) #返回索引,找不到报错 list1=[1,1,2,33,44,30,19] print(max(list1)) print(min(list1)) |
6.删除
1 2 3 4 5 |
list1=["a","b","c","d","e",1,1,2,33,44,30,19] list1.pop() #索引 删除最后一个元素 list1.pop(0) list1.remove('c') print(list1) |
7.排序
1 2 3 4 5 |
list1=["a","b","c","d","e"] list1.sort() print(list1) list1.sort(reverse=True) print(list1) |
二、运算符
1.成员运算符
In 或者 not in字符串或集合中是否包含
1 2 3 |
list1=[1,"张三1","男"] print("张三1" in list1) print("张三1" not in list1) |
2.身份运算符
is : 判断标示符是不是引用同一个对象
Is not: 判断标示符是不是引用不同一个对象
1 2 3 4 5 6 7 8 9 10 11 12 |
list1=[1,"张三1","男"] list2=list1 #list1赋值给list2 print(id(list2),id(list1)) print(list1 is list2) #引用 内存地址 print(list1 is not list2) print("‐‐"*20) print(list1==list2) print("‐‐"*20) list3=[1,"张三1","男"] print(id(list1),id(list3)) print(list1==list3) #T print(list1 is list3) #F |
总结: is 比较的内存地址 id()查看内存地址
== 比较值
- 我的微信
- 这是我的微信扫一扫
- 我的微信公众号
- 我的微信公众号扫一扫