unpack the first two elements in list/tuple

后端 未结 5 1434
小蘑菇
小蘑菇 2020-12-29 01:30

Is there a way in python to do like this:

a, b, = 1, 3, 4, 5

And then:

>>> a
1
>>> b
3

5条回答
  •  清歌不尽
    2020-12-29 02:05

    Or in Python 3.x you could do this:

      a, *b = 1, 3, 4
    

    giving you:

    In [15]: a
    Out[15]: 1
    
    In [16]: b
    Out[16]: [3, 4]
    

    It would avoid the exception, though you would have to parse b. This assume that you only want to have two variables on the left of the =, otherwise you could use

    a, b, *ignore = ....
    

    with v3.x

提交回复
热议问题