parse a list into variable and sublist

I am parsing a list into a variable and another list with this script:

b=[' 687.3774', ' 478.6', ' 47', ' 58', ' 96.90']
c,d=b[0],b[1:]

It is always the first element that would be separated and this code works fine, however, it repeats the list b on the right hand side. This is not a problem but it does get annoying when my b is something big like line.replace('*',',').replace(' ',',').split(',') . This doesn't really look like the pythonic way of writing this down. I have read some posts in this forum and the documentation on tuples and etc but nothing quite did the trick for me. Below are some things that I tried in a "shot in the dark" manner and that obviously didn't work

d=[]
c,d[:]=b
c,list(d)=b
c,d=b[0],[1:]

I am also aware of the b.pop method but I could not find a method to use that without repeating b in the RHS.

Help is appreciated. Thank you.


In Python 3, you can try

c, *d = b

It will assign b[0] to c and the rest to d . You should see this question's answers for an explanation on how the * operator works on sequences.

链接地址: http://www.djcxy.com/p/53568.html

上一篇: 将循环打包成一个函数来静音变量的pythonic方法?

下一篇: 将一个列表解析成变量和子列表