python可迭代对象拆包

python可迭代对象进行拆包,可省略的元素可以用*代替

例如:

list5 = ["I","love","python",(1,2,3),'a','b',456]
str3, *other, num1 = list5
print(str3,type(str3))# I <class 'str'>
print(other,type(other))# ['love', 'python', (1, 2, 3), 'a', 'b'] <class 'list'>
print(num1,type(num1))# 456 <class 'int'>

拆包一些元素后丢弃它们,使用* _ 或者 ign 。(规范来的,你也可以用其他变量名)

 

posted @ 2020-12-13 14:36  echo'coding'  阅读(105)  评论(0编辑  收藏  举报