Python: unpack to unknown number of variables?

后端 未结 3 1181
小蘑菇
小蘑菇 2021-01-04 07:14

How could I unpack a tuple of unknown to, say, a list?

I have a number of columns of data and they get split up into a tuple by some function. I want to unpack this

3条回答
  •  有刺的猬
    2021-01-04 07:30

    You can use the asterisk to unpack a variable length. For instance:

    foo, bar, *other = funct()
    

    This should put the first item into foo, the second into bar, and all the rest into other.

    Update: I forgot to mention that this is Python 3.0 compatible only.

提交回复
热议问题