effective python读书笔记——带*的表达式
#当需要把数据拆分赋给变量时:
nums=[2,5,1,7,4,9,8,3,0]
nums.sort(reverse=True)
first=nums[0]
second=nums[1]
others=nums[2:]
print(f"first : {first} second : {second} others : {others}")
#切片法显得比较乱,还得考虑下标
first_1,second_1,*others_1=nums #清晰
print(f"first_1 : {first_1} second_1 : {second_1} others_1 : {others_1}")
#可以捕获任何一段元素
a,*b,c=nums
print(f"a : {a} b : {b} c : {c}")
#带星号的变量至少与一个普通变量搭配,且单层结构只能出现一次
#如 *d=nums *e,*f,g=nums 就会出错
#多层结构也能用,但是不推荐:
nums_1={'first':(1,2,3,4),'second':(4,5,6)}
((x,(x1,*x2)),(y,(y1,*y2)))=nums_1.items()
print(f'{x} have {x1} and {x2} \n{y} have {y1} and {y2}')
#可生成空列表
short=[1,2]
a,b,*c=short
print(f'{a}--{b}--{c}')
