优快云话题挑战赛第2期
参赛话题:学习笔记
学习之路,长路漫漫,写学习笔记的过程就是把知识讲给自己听的过程。这个过程中,我们去记录思考的过程,便于日后复习,梳理自己的思路。学习之乐,独乐乐,不如众乐乐,把知识讲给更多的人听,何乐而不为呢
day06-python-list、tuple学习
一、List
1. 列表概述
list底层是动态数组实现,是多个值构成的序列,是可变数据类型,列表中的数据类型可以是任意数据类型。
2.列表的构成语法
[元素1,元素2,元素3…]
注意其中元素的开始下标是从0开始,末尾下标是-1,与字符串类似 。且元素类型可以为python任意数据类型
举例:
lista = ['a', True, 1, 'bbb', 1.0, 1 + 2j, [3, 5, 'a']] #定义一个包含多种数据类型的列表
print(lista)
print(lista[0]) #输出列表第一个元素
print(lista[-1]) #输出最后一个元素
输出结果:
['a', True, 1, 'bbb', 1.0, (1+2j), [3, 5, 'a']]
a
[3, 5, 'a']
3.列表的创建
3.1空列表创建
list1=[]
list2=list()
3.2列表带元素的初始化创建
#带元素初始化
list2=[1,2,3,'ab']
list3=list(list2)
从已有的列表通过切片创建列表
list4=list2[:]
3.3通过拷贝创建
import copy
list1 = ['a', True, 1, 'bbb', 1.0, [1, 2, 3, 4], 1 + 2j]
list2 = list1
list3 = list1.copy()
list4 = copy.deepcopy(list1)
print("list1:", list1, id(list1))
print("list2:", list2, id(list2))
print("list3:", list3, id(list3))
print("list4:", list4, id(list4))
输出结果:
list1: ['a', True, 1, 'bbb', 1.0, [1, 2, 3, 4], (1+2j)] 1850621355008
list2: ['a', True, 1, 'bbb', 1.0, [1, 2, 3, 4], (1+2j)] 1850621355008
list3: ['a', True, 1, 'bbb', 1.0, [1, 2, 3, 4], (1+2j)] 1850618799936
list4: ['a', True, 1, 'bbb', 1.0, [1, 2, 3, 4], (1+2j)] 1850618800512
过程分析工具:python Tutor
过程分析:
3.3.1引用拷贝、浅拷贝、深拷贝
结果说明:
1.list2=list1 输出结果的id一样, 且图中可以看出list1与list2是共用同一块内存区域的 。这种拷贝方式被称为引用拷贝,相当于快捷方式的创建。
2.list3 = list1.copy() ,这种拷贝方式list3与list1的输出ID不一样了,且从图中也可以看出是list3指向了新的内存区域。但新内存区域中的位置5和位置6还是指向原来的元素list与complex。这种拷贝方式是不完全的内存拷贝为浅拷贝。而之所以没有为list1中list元素与complex开辟新的内存空间,
主要原因:
是浅拷贝的内层如果嵌套了可变对象,那浅拷贝是不会复制出来一个新的可变对象,【毕竟只是浅浅的拷贝…】
complex之所以没有开辟新的内存空间,那可不能怪浅拷贝了,那是因为complex本身就是不可变对象呀[类比元组],即使你让深拷贝上它也还是不行呀。
3.list4 = copy.deepcopy(list1) 这种拷贝方式可以从图中出,拷贝已经为嵌套的list开辟新的内存,这也是深拷贝与浅拷贝的差异:深拷贝不管可变对象多深一定是依次复制出来一个新的对象。
4 列表的增删改查
4.1 列表的增操作
4.1.1 列表元素复制
列表2 = 列表1 *n
list1 = ['hello', 'ni']
list2 = list1 * 3 # 重复3次
print(list2)
#输出结果:['hello', 'ni', 'hello', 'ni', 'hello', 'ni']
*注意 运算是返回一个新的列表!!!,不是在原列表上做变更。
4.1.2 列表的+ 、extend()方法
列表1 = 列表2 + 列表3
list.extend(列表/元组)
list1 = ['hello', 'ni']
list2 = [1, 2, 3]
print("list1:", list1, id(list1))
print("list1 + list2:", list1 + list2, id(list1 + list2))
list1.extend(list2) #无返回值呀!!,该方法直接修改list1
print("list1.extend(list2):", list1, id(list1))
分析:
结果:
list1: ['hello', 'ni'] 2695388124096
list1 + list2: ['hello', 'ni', 1, 2, 3] 2695388653632
list1.extend(list2): ['hello', 'ni', 1, 2, 3] 2695388124096
输出结果可以看出 extend 方法与+方法都能实现list的增操作,但+运算是产生了新的列表,而extend是在原list上修改
4.1.3 列表的insert()与append()方法
这两种方法都是在原列表上操作。
-
list.insert(下标值, 元素/列表)
-
list.append(元素/列表)
list1 = ['hello', 'ni', 'hao']
print("old:", list1)
list1.insert(1, "python")
print("insert:", list1)
list1.append(1234)
print("append:", list1)
输出结果:
old: ['hello', 'ni', 'hao']
insert: ['hello', 'python', 'ni', 'hao']
append: ['hello', 'python', 'ni', 'hao', 1234]
过程分析:
4.2 列表的删除操作
4.2.1 元素的删除
-
list.pop(下标值) 、 list.pop() 返回删除数据、
-
list.remove(元素) 不返回删除数据
-
del 元素
使用举例:
list1 = ['hello', 'ni', 'hao', 11, 22, 33]
print(list1.pop())
print(list1)
print(list1.remove('hello'))
print(list1)
del list1[1]
print(list1)
输出结果:
33
['hello', 'ni', 'hao', 11, 22]
None
['ni', 'hao', 11, 22]
['ni', 11, 22]
4.2.2 整个列表的删除
-
list.clear()
-
del 列表
使用举例:
list1 = ['hello', 'ni', 'hao', 11, 22, 33]
list2 = [11, 22, 33]
list1.clear()
print("list1:", list1)
del list2
print("list2:", list2)
输出结果:
list1: []
Traceback (most recent call last):
File "D:\python_projects\day06.py", line 28, in <module>
print("list2:", list2)
NameError: name 'list2' is not defined. Did you mean: 'list1'?
结果表明 clear()方法是清空列表的所有元素,而del是将整个列表完全从内存中删除
4.3 列表的元素更新操作
-
list[索引]=新值
-
list.insert(索引,新值)
list1 = ['hello', 'ni', 'hao', 11, 22, 33]
print(list1)
list1.insert(1, 'HI') #insert可以用于更新元素操作
print(list1)
list1[3] = "HAHA"
print(list1)
输出结果:
['hello', 'ni', 'hao', 11, 22, 33]
['hello', 'HI', 'ni', 'hao', 11, 22, 33]
['hello', 'HI', 'ni', 'HAHA', 11, 22, 33]
4.4 列表的查询
4.4.1 单个元素查询
-
查看指定索引的元素 list[索引]
-
查询某个元素是否在列表中 : in / not in
-
list.index(object[, start】[, stop]) 从指定的范围的列表中找出某个值(object)返回第一次匹配的索引值,其中start,stop是左闭右开
list1 = ['hello', 'ni', 'hao', 11, 'ni', 33]
print(list1[1]) #输出 ni
print(11 in list1) #输出 True
print(list1.index('ni')) #输出 1 返回第一次匹配的索引值
print(list1.index('ni', 2, -1)) #输出 4 因为指定了位置
print(list1.index('ni', 2, -2)) #抛出异常 ,因为左闭右开找不到
4.4.2 列表的遍历的五种方式
方式1: for 变量元素 in 列表:
使用示例:
list1 = ['hello', 'ni', 'hao', 11, 'ni', 33]
for var in list1:
print(var, end=' ')
#输出结果: hello ni hao 11 ni 33
方式2: for index,变量元素 in enumerate(列表)
使用示例:
list1 = ['hello', 'ni', 'hao', 11, 'ni', 33]
for index, var in enumerate(list1):
print(index, var, end=' ', sep=':') # 返回index 与value
#输出结果:0:hello 1:ni 2:hao 3:11 4:ni 5:33
方式3: for index in range(len(列表)):
使用示例:
list1 = ['hello', 'ni', 'hao', 11, 'ni', 33]
for index in range(len(list1)):
print(list1[index], end=' ')
#输出结果:hello ni hao 11 ni 33
方式4: for 变量元素 in iter(列表):
使用示例:
list1 = ['hello', 'ni', 'hao', 11, 'ni', 33]
for var in iter(list1):
print(var, end=' ') # iter 常常配合next()使用
#输出结果:hello ni hao 11 ni 33
方式5: while index <len(列表)
使用示例:
list1 = ['hello', 'ni', 'hao', 11, 'ni', 33]
index=0
while index < len(list1):
print(list1[index], end=' ')
index += 1
#输出结果:hello ni hao 11 ni 33
5. 列表的聚合方法使用
- list.count(元素) #查看元素在列表中出现的次数
list1 = [1, 2, 3, 1, 3, 5, 6, 7, 8]
list2 = ['ab', 'cdd', 'ea', 'bbc']
print("list1.count(3):", list1.count(3))
print("list2.count('abc'):", list2.count('abc'))
结果:
list1.count(3): 2
list2.count('abc'): 0
- len(list) #获取元素列表个数(长度)
list1 = [1, 2, 3, 1, 3, 5, 6, 7, 8]
list2 = ['ab', 'cdd', 'ea', 'bbc']
print("len(list1):", len(list1))
print("len(list2):", len(list2))
输出结果:
len(list1): 9
len(list2): 4
- max(list) #获取列表中的最大值
list1 = [1, 2, 3, 1, 3, 5, 6, 7, 8]
list2 = ['ab', 'cdd', 'ea', 'bbc']
print("max(list1):", max(list1))
print("max(list2):", max(list2)) #ascii码比较
输出结果:
max(list1): 8
max(list2): ea
- min(list) #获取列表中的最小值
list1 = [1, 2, 3, 1, 3, 5, 6, 7, 8]
list2 = ['ab', 'cdd', 'ea', 'bbc']
print("min(list1):", min(list1))
print("min(list2):", min(list2)) # ascii码比较
输出结果:
min(list1): 1
min(list2): ab
- 特殊情况说明:
list1 = [1, 2, 3, 1, 3, 5, 6, 7, 8, 'a', 'b']
print("len(list1):", len(list1))
print("list1.count(1):", list1.count(1))
print("min(list1):", min(list1))
print("max(list1):", max(list1))
输出结果:
len(list1): 11
list1.count(list1): 2
Traceback (most recent call last):
File "D:\python_projects\day06.py", line 50, in <module>
print("min(list1):", min(list1))
TypeError: '<' not supported between instances of 'str' and 'int'
list中的数据类型如果不可比较max 、min会报错!!
6. list排序操作:
list中的元素倒叙输出,是在原列表上操作
list.reverse()
list1 = [1, 6, 2, 3, 0]
list1.reverse()
print(list1) #输出[0, 3, 2, 6, 1]
对list的元素进行排序(稳定排序)。可通过提供比较函数cmp、key(创建用户排序的键)和降序标志reverse(一个布尔值)进行定制
list.sort([cmp][,key][,reverse])
简单使用:
list1 = [1, 6, 2, 3, 0]
list1.sort(reverse=True) #指定反序
print(list1) #输出[0, 3, 2, 6, 1]
sorted(列表) #返回一个新的列表
list1 = [1, 6, 2, 3, 0]
list2=sorted(list1)
print(list2) #输出[0, 1, 2, 3, 6]
注意 sorted函数与sort方法是不能对即含有数字又含有字符串的列表排序的,否则会报TypeError
切片反序输出:
list1 = [1, 2, 3, 1, 3, 5, 6, 7, 8, 'a', 'b']
list2=list1[:-1] #返回新的列表
print(list2) #输出结果 ['b', 'a', 8, 7, 6, 5, 3, 1, 3, 2]
7.列表配合random的使用
random.choice(列表) #返回一个随机选择的元素
import random
pets = ['Dog', 'Cat','Moose','Bird','Fish']
print(random.choice(pets))
random.shuffle(列表) #对列表中的元素重新洗牌排序,修改原列表,并返回新的列表
import random
pets = ['Dog', 'Cat','Moose','Bird','Fish']
random.shuffle(pets)
print(pets)
二、Tuple
1.元组的概述
-
元组是静态数组,不可变,且其内部数据一旦创建便无法改变。
-
元组缓存于Python运行时环境,这意味着我们每次使用元组时无须访问内核去分配内存。
2.元组与列表的差异
-
元组输入时用的(),列表是[]
-
元组与字符串一样是不可变类型,列表是可变类型
可以使用 list()与tuple()实现列表与元组的转换元组的创建
3.元组的创建
#空元组
t1=()
#非空元组:
t1=1, #如果元组只有一个元素要带上逗号告诉python解释器这是一个元组,其实元组可以不用带括号
t2=(1,)
t3 =(1, 2, 3)
t4=1,2,3 #其实元组可以不用带括号
tuple(iterable) 用可迭代对象生成一个元组
4.元组的使用
基本与list一样
注意 元组不可变所以肯定不能在原元组上对元素做增、删、改的操作,所以上面的列表中增、删、改方法且返回原列表的方法,元组是不支持的。但如果列表中的操作是返回新的列表,那元组还是支持的呀。
列如:
4.1 切片、加运算 、乘运算
tuple1 = (1, 2, 3, 4, 5, 'qw')
tuple2 = tuple1[1::2]
tuple3 = tuple1 * 2
tuple4 = tuple1 + tuple2
print("tuple1:", tuple1)
print("tuple2:", tuple2)
print("tuple3:", tuple3)
print("tuple4:", tuple4)
输出结果:
tuple1: (1, 2, 3, 4, 5, 'qw')
tuple2: (2, 4, 'qw')
tuple3: (1, 2, 3, 4, 5, 'qw', 1, 2, 3, 4, 5, 'qw')
tuple4: (1, 2, 3, 4, 5, 'qw', 2, 4, 'qw')
4.2 逻辑判断符
in /not in
4.3 查询、聚合、排序常用方法
查询、聚合、排序常用方法当然元组跟列表还是一样是支持的呀,上面列表中已经对这些方法做了介绍与简单使用,元组就不再赘述。
注意:元组排序当然没有list.sort()这种方法了呀,但支持sorted函数哦,因为前sort方法是在原列表上操作,所以不支持呀!!
4.4 tips -查询元组相关函数
dir(tuple) 、help(tuple)
三、参考资料
官方文档:sequence-types-list-tuple-range
运行内存可视化网站:python Tutor