-
2 votesanswersviews
为什么'Extended Iterable Unpacking'不能用于空字符串?
为什么是 >>> a, *b = '' 不可能,何时 >>> a, *b = ' ' >>> a, b (' ', []) # b == empty list here anyway. 和 >>> type('') <class 'str'> 我的意思是,为什么不呢 >>> a, *b =... -
2 votesanswersviews
为什么函数参数中的元组赋值在python3中不起作用
在Python3中,以下代码不再起作用: def function((a, (b, c))): pass 为什么删除它?什么原因? -
21 votesanswersviews
解压缩一般化
>>> LOL = [[1, 2], ['three']] >>> [*LOL[0], *LOL[1]] [1, 2, 'three'] 好的!再见itertools.chain . 反正从来没有喜欢过你 . >>> [*L for L in LOL] File "<ipython-input-21-e86d2c09c33... -
1 votesanswersviews
为什么逗号分隔的iterables不能像zip那样工作?
我想了解的是为什么以下代码 for x, y in [1,2], [3,4]: print(x, y, x + y) 版画 1 2 3 3 4 7 代替 1 3 4 2 4 6 现在,我知道zip工作和not using zip to iterate over a pair of lists could be considered an anti-pattern,但我仍然需要对此默认... -
236 votesanswersviews
“解包”一个元组来调用匹配的函数指针
我试图在_363229中存储不同数量的值,这些值稍后将用作调用与存储类型匹配的函数指针的参数 . 我创建了一个简化的示例,显示了我正在努力解决的问题: #include <iostream> #include <tuple> void f(int a, double b, void* c) { std::cout << a << ":... -
3 votesanswersviews
Python中生成器解包的奇怪行为
我目前正在尝试更熟悉Python中的迭代器,我遇到了一些奇怪的行为 . 从本质上讲,我使用生成器理解得到了错误的行为,但是使用列表理解的正确行为 . Let me start by explaining what I try to do, and then what behaviour I get.想象一下,有一个可迭代的字典,例如, d = {'a': [1, 2, 3], 'b': [4, 5... -
81 votesanswersviews
拆包,扩展拆包和嵌套扩展拆包
考虑一下这些表达方式......请耐心等待...这是一个很长的清单...... (注意:重复一些表达式 - 这只是为了呈现“上下文”) a, b = 1, 2 # simple sequence assignment a, b = ['green', 'blue'] # list asqignment a, b = 'XY'... -
9 votesanswersviews
Python元组解包
如果我有 nums_and_words = [(1, 'one'), (2, 'two'), (3, 'three')] 并希望 nums = [1, 2, 3] words= ['one', 'two', 'three'] 我怎么用Pythonic方式做到这一点?我花了一分钟才意识到为什么以下不起作用 nums, words = [(el[0], el[1]) for el in nums_... -
2 votesanswersviews
列表理解中的奇怪解包[重复]
这个问题在这里已有答案: What does ** (double star/asterisk) and * (star/asterisk) do for parameters? 18个答案 我在看a lecture from David Beazley . 在23:20分钟,他做了一些"magic"解压,我很难理解 . “魔术线”是 fail = [ { **row, '... -
58 votesanswersviews
Python元组在return语句中解压缩
Python语言(尤其是3.x)允许对迭代的非常一般的解包,其中一个简单的例子就是 a, *rest = 1, 2, 3 多年来,这种拆包已经逐渐推广(参见例如PEP 3132和PEP 448),允许它在越来越多的情况下使用 . 因此,我惊讶地发现以下是Python 3.6中的无效语法(并且在Python 3.7中仍然如此): def f(): rest = [2, 3] ret...