简体   繁体   English

使用序列解包的ipython怪异行为

[英]ipython weird behaviour with sequence unpacking

I have a weird behavior with sequence unpacking in ipython 我在ipython中使用序列解包有一个奇怪的行为

In [12]: items = [1, 10, 7, 4, 5, 9]

In [13]: head, *tail = items
  File "<ipython-input-13-34256df22cca>", line 1
    head, *tail = items
          ^
SyntaxError: invalid syntax

This syntax ( PEP 3132 - Extended Iterable Unpacking ) was introduced in Python 3.0. 这种语法( PEP 3132 - 扩展可迭代解包 )是在Python 3.0中引入的。 Check you python version. 检查你的python版本。

In Python 3.3: 在Python 3.3中:

>>> items = [1, 10, 7, 4, 5, 9]
>>> head, *tail = items
>>> head
1
>>> tail
[10, 7, 4, 5, 9]

In Python 2.7, it raises SyntaxError: 在Python 2.7中,它引发了SyntaxError:

>>> items = [1, 10, 7, 4, 5, 9]
>>> head, *tail = items
  File "<stdin>", line 1
    head, *tail = items
          ^
SyntaxError: invalid syntax
>>> head, tail = items[0], items[1:] # workaround
>>> head
1
>>> tail
[10, 7, 4, 5, 9]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM