python decompression sequence

Any sequence / iterative object can be assigned to a plurality of variables and decompressed by the assignment statement.

>>> a,b,c = 'hel'
>>> a
'h'
>>> 
>>> b
'e'
>>> c
'l'
>>> s_iter = iter(s)
>>> a,b,c = s_iter
>>> a
'h'
>>> b
'e'
>>> 
>>> c
'l'

 

But the premise is the number of variables must be the same with the number of sequence elements.

>>> a,b,c = "hello"
Traceback (most recent call last):
  File "<input>", line 1, in <module>
ValueError: too many values to unpack (expected 3) #hello 5个去解压不行!

 

Now demand is: Given a sequence, and then find its beginning and the last one , (can not use the index!)

1 >>> a,*_,c = l
2 >>> a
3 10
4 >>> c
5 7
6 >>> _
7 [3, 5, 6, 8, 9, 7, 5, 4, 1, 10, 20, 5, 6]

 

Guess you like

Origin www.cnblogs.com/zach0812/p/11333102.html