问题描述
>>> LOL = [[1, 2], ['three']]
>>> [*LOL[0], *LOL[1]]
[1, 2, 'three']
好的!再见 itertools.chain
.无论如何,从没喜欢过你.
Alright! Goodbye itertools.chain
. Never liked you much anyway.
>>> [*L for L in LOL]
File "<ipython-input-21-e86d2c09c33f>", line 1
[*L for L in LOL]
^
SyntaxError: iterable unpacking cannot be used in comprehension
哦.为什么我们不能拥有美好的事物?
Oh. Why can't we have nice things?
解压缩似乎是很明显的/Python式的,但是由于他们不愿意添加该特殊错误消息,因此有理由将其禁用.那么,该语法有什么问题?
Unpacking in a comprehension seems to be obvious/pythonic, but since they've bothered to add that special error message there was a reason for disabling it. So, what's the problem with that syntax?
推荐答案
我还窥视了该功能的Python问题跟踪器.我发现一个问题,在实施过程中进行了讨论. 此处的一系列信息帮助他们实现了这一目标,并很好地介绍了歧义在 msg234766 中由GvR提出.
I also took a peek at the Python issue tracker for this feature. I found an issue in which discussion took place while implementing it. The sequence of messages that helped them come to this realization starts here with a nice overview of the ambiguity introduced presented in msg234766 by GvR.
在link-rot的 fear 中,我在此处附加(格式化的)消息:
In fear of link-rot, I'm attaching the (formatted) message here:
def f(*a, **k): print(list(a), list(k))
然后我们可以尝试以下操作:
Then we can try things like:
f(x for x in ['ab', 'cd'])
打印一个生成器对象,因为它被解释为一个生成器表达式的参数.
which prints a generator object, because this is interpreted as an argument that's a generator expression.
但是现在让我们考虑:
f(*x for x in ['ab', 'cd'])
我个人希望这等同于:
f(*'ab', *'cd')
IOW:
f('a', 'b', 'c', 'd')
PEP在此没有明确说明要做什么.现在的问题是,我们应该将诸如*x for x in ...
之类的内容解释为生成器表达式的扩展形式,还是作为*arg
的扩展形式?我不知何故认为后者更有用,而且逻辑扩展也更合理.
The PEP doesn't give clarity on what to do here. The question now is, should we interpret things like *x for x in ...
as an extended form of generator expression, or as an extended form of *arg
? I somehow think the latter is more useful and also the more logical extension.
我的理由是PEP支持f(*a, *b)
之类的东西,并且将f(*x for x in xs)
解释为对列表xs
中的每个x
做*x
事情是很合逻辑的.
My reasoning is that the PEP supports things like f(*a, *b)
and it would be fairly logical to interpret f(*x for x in xs)
as doing the *x
thing for each x
in the list xs
.
因此,我们可能很快就会看到它(虽然不是:-,绝对不是3.6,),但我希望我们这样做,它们看起来不错.
这篇关于解开概括的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!