8 元组
8.1 元组定义、切片、相关操作
code:
# *********************************************************
"""
元组:
元组定义:
方法1:使用小括号定义,元素之间用逗号分隔
方法2:不用括号,元素之间用逗号分隔
方法3:使用tuple()函数定义
注意事项:元组是一种有序的存储类型,但是元组元素定义后不可更改。
如果元组只有一个元素的话,一定要加逗号,不然就会自动生成元素类型的变量而不是元组。
元组切片:
tuple[begin:end:step]:begin表示起始索引,end表示终止索引,step表示步进值,[begin:end)--是一个前开后闭区间
begin的默认值为0,end的默认值为元组长度,step的默认值为1
元组相关操作:
len(tuple)---返回元组tuple的长度
tuple.count(x)---返回x在元组tuple中出现的次数
s.index(x[, i[, j]])---x在s中首次出现项的索引号(索引号在i或其后且在j之前),如果寻找不到x,则引发异常。其中i的默认值为0,j的默认值为元组长度
+---将两个元组的元素合并为一个元组
*---将元组重复n次
in---判断元素是否在元组中,在返回True,不在返回false
not in---判断元素是否不在元组中,在返回False,不在返回True
"""
print("元组定义*****************************************")
tuple1 = (1,2,3,4)
print(f"tuple1 = {tuple1}")
tuple2 = (1,)
print(f"tuple2 = {tuple2}")
tuple3 = 1,2,3,4,5,6
print(f"tuple3 = {tuple3}")
tuple4 = 1,
print(f"tuple4 = {tuple4}")
tuple5 = tuple()
print(f"tuple5 = {tuple5}")
print("元组切片*****************************************")
print(f"tuple1[:] = {tuple1[:]}")
print(f"tuple1[::] = {tuple1[::]}")
print(f"tuple1[::2] = {tuple1[::2]}")
print(f"tuple1[::-1] = {tuple1[::-1]}")
print("元组相关操作*****************************************")
print(f"len(tuple1) = {len(tuple1)}")
print(f"tuple1.index(4,0,4) = {tuple1.index(4,0,4)}")
print(f"tuple1.index(4) = {tuple1.index(4)}")
tuple6 = tuple1 + tuple2
print(f"tuple1 + tuple2 = {tuple6}")
tuple7 = tuple1 * 3
print(f"tuple1 * 3 = {tuple7}")
print(f"4 in tuple1:{4 in tuple1}")
print(f"4 not in tuple1:{4 not in tuple1}")
运行结果:
E:\Programs_Way\Python\python.exe D:/Prj/Python/Study_Basic_Grammar/_39tuple_definition_slice_function.py
元组定义*****************************************
tuple1 = (1, 2, 3, 4)
tuple2 = (1,)
tuple3 = (1, 2, 3, 4, 5, 6)
tuple4 = (1,)
tuple5 = ()
元组切片*****************************************
tuple1[:] = (1, 2, 3, 4)
tuple1[::] = (1, 2, 3, 4)
tuple1[::2] = (1, 3)
tuple1[::-1] = (4, 3, 2, 1)
元组相关操作*****************************************
len(tuple1) = 4
tuple1.index(4,0,4) = 3
tuple1.index(4) = 3
tuple1 + tuple2 = (1, 2, 3, 4, 1)
tuple1 * 3 = (1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4)
4 in tuple1:True
4 not in tuple1:False
Process finished with exit code 0
8.2 元组推导式
code:
# ************************************************
"""
元组推导式(生成器):
列表推导式结果返回了一个列表,元组推导式返回的是一个生成器
语法:
列表推导式---[变量 for i in 容器]---返回的是一个列表
元组推导式---(变量 for i in 容器)---返回的是一个生成器
生成器是什么?
生成器是一个特殊的迭代器,生成器可以自定义,也可以使用元组推导式定义
生成器是按照某种算法去推算下一个数据或结果,只需要在内存中存储一个生成器,节省内存,提升性能
定义生成器的语法:
1.里面是推导式,外面是一个()的结果就是一个生成器
2.自定义生成器:含有yield关键字的函数就是生成器
含有yield关键字的函数,返回的结果是一个迭代器,换句话说,生成器就是一个返回迭代器的函数
如何操作生成器?
是迭代器的一种,和迭代器的操作方式一样:next() list() tuple() for循环---特点就是取完就没有了
"""
varlist = [1,2,3,4,5,6,7,8,9]
newlist = (i**2 for i in varlist)
print(f"newlist = {newlist}")
print(f"next(newlist) = {next(newlist)}")
print(f"list(newlist) = {list(newlist)}")
newlist = (i**2 for i in varlist)
print(f"tuple(newlist) = {tuple(newlist)}")
运行结果:
E:\Programs_Way\Python\python.exe D:/Prj/Python/Study_Basic_Grammar/_40tuple_comprehension.py
newlist = <generator object <genexpr> at 0x000001FF712109E0>
next(newlist) = 1
list(newlist) = [4, 9, 16, 25, 36, 49, 64, 81]
tuple(newlist) = (1, 4, 9, 16, 25, 36, 49, 64, 81)
Process finished with exit code 0
8.3 生成器函数
code:
# *************************************************************
"""
yield关键字:专门用于生成器函数中;调用生成器函数,返回一个迭代器;所得迭代器取出一个元素,函数执行一段内容直到下一个yield关键字,返回yield返回的值
如果使用list() or tuple()函数取出迭代器的数据,则返回一个返回值组成的列表 or 元组,并执行生成器函数的所有内容
yield和函数中的return有点像
共同点:执行到yield和return之后会把结果返回
不同点:return会把结果返回,并结束当前函数的调用,yield会返回结果,并记住当前代码执行的位置,下一次调用时会从上一次离开的位置继续向下执行
注意:如果所有的yield都执行完毕,再次调用yield函数时会报错
作用:对于一次无法处理完成的大量数据,可以使用生成器,调用一次yield函数取出一部分数据,直到数据取出完成
"""
def hello():
print('hello return')
return 1
print('hello 2') # 后面这两句绝对不会执行
return 2
def hello1():
print('hello yield 1')
yield 1
print('hello yield 2')
yield 2
print('hello yield 3') # 使用list() or tuple()函数可以执行,如果调用三次next()的话会报错
print("return*************************************")
print(f"调用hello():{hello()}")
print("yield*************************************")
res = hello1() # res是一个迭代器
r = next(res) # 取出一个值,返回第一个yield返回的值
print(f"r = next(res) = {r}")
r = next(res)
print(f"r = next(res) = {r}")
res = hello1() # res是一个迭代器
print(f"list(res) = {list(res)}") # 返回一个列表
res = hello1() # res是一个迭代器
print(f"tuple(res) = {tuple(res)}") # 返回一个列表
# 举例:使用生成器生成斐波那契数列
def fibo():
a,b = 0,1
while(1):
yield a
a,b = b,a+b
res = fibo()
num = int(input("请输入一个正整数:"))
for i in range(num):
print(next(res),end=' ')
运行结果:
E:\Programs_Way\Python\python.exe D:/Prj/Python/Study_Basic_Grammar/_41generator_yield_keyword.py
return*************************************
hello return
调用hello():1
yield*************************************
hello yield 1
r = next(res) = 1
hello yield 2
r = next(res) = 2
hello yield 1
hello yield 2
hello yield 3
list(res) = [1, 2]
hello yield 1
hello yield 2
hello yield 3
tuple(res) = (1, 2)
请输入一个正整数:10
0 1 1 2 3 5 8 13 21 34
Process finished with exit code 0