考虑以下表达式。请注意,某些表达式被重复以呈现“上下文”。
(这是一个很长的 list )
a, b = 1, 2 # simple sequence assignment
a, b = ['green', 'blue'] # list asqignment
a, b = 'XY' # string assignment
a, b = range(1,5,2) # any iterable will do
# nested sequence assignment
(a,b), c = "XY", "Z" # a = 'X', b = 'Y', c = 'Z'
(a,b), c = "XYZ" # ERROR -- too many values to unpack
(a,b), c = "XY" # ERROR -- need more than 1 value to unpack
(a,b), c, = [1,2],'this' # a = '1', b = '2', c = 'this'
(a,b), (c,) = [1,2],'this' # ERROR -- too many values to unpack
# extended sequence unpacking
a, *b = 1,2,3,4,5 # a = 1, b = [2,3,4,5]
*a, b = 1,2,3,4,5 # a = [1,2,3,4], b = 5
a, *b, c = 1,2,3,4,5 # a = 1, b = [2,3,4], c = 5
a, *b = 'X' # a = 'X', b = []
*a, b = 'X' # a = [], b = 'X'
a, *b, c = "XY" # a = 'X', b = [], c = 'Y'
a, *b, c = "X...Y" # a = 'X', b = ['.','.','.'], c = 'Y'
a, b, *c = 1,2,3 # a = 1, b = 2, c = [3]
a, b, c, *d = 1,2,3 # a = 1, b = 2, c = 3, d = []
a, *b, c, *d = 1,2,3,4,5 # ERROR -- two starred expressions in assignment
(a,b), c = [1,2],'this' # a = '1', b = '2', c = 'this'
(a,b), *c = [1,2],'this' # a = '1', b = '2', c = ['this']
(a,b), c, *d = [1,2],'this' # a = '1', b = '2', c = 'this', d = []
(a,b), *c, d = [1,2],'this' # a = '1', b = '2', c = [], d = 'this'
(a,b), (c, *d) = [1,2],'this' # a = '1', b = '2', c = 't', d = ['h', 'i', 's']
*a = 1 # ERROR -- target must be in a list or tuple
*a = (1,2) # ERROR -- target must be in a list or tuple
*a, = (1,2) # a = [1,2]
*a, = 1 # ERROR -- 'int' object is not iterable
*a, = [1] # a = [1]
*a = [1] # ERROR -- target must be in a list or tuple
*a, = (1,) # a = [1]
*a, = (1) # ERROR -- 'int' object is not iterable
*a, b = [1] # a = [], b = 1
*a, b = (1,) # a = [], b = 1
(a,b),c = 1,2,3 # ERROR -- too many values to unpack
(a,b), *c = 1,2,3 # ERROR - 'int' object is not iterable
(a,b), *c = 'XY', 2, 3 # a = 'X', b = 'Y', c = [2,3]
# extended sequence unpacking -- NESTED
(a,b),c = 1,2,3 # ERROR -- too many values to unpack
*(a,b), c = 1,2,3 # a = 1, b = 2, c = 3
*(a,b) = 1,2 # ERROR -- target must be in a list or tuple
*(a,b), = 1,2 # a = 1, b = 2
*(a,b) = 'XY' # ERROR -- target must be in a list or tuple
*(a,b), = 'XY' # a = 'X', b = 'Y'
*(a, b) = 'this' # ERROR -- target must be in a list or tuple
*(a, b), = 'this' # ERROR -- too many values to unpack
*(a, *b), = 'this' # a = 't', b = ['h', 'i', 's']
*(a, *b), c = 'this' # a = 't', b = ['h', 'i'], c = 's'
*(a,*b), = 1,2,3,3,4,5,6,7 # a = 1, b = [2, 3, 3, 4, 5, 6, 7]
*(a,*b), *c = 1,2,3,3,4,5,6,7 # ERROR -- two starred expressions in assignment
*(a,*b), (*c,) = 1,2,3,3,4,5,6,7 # ERROR -- 'int' object is not iterable
*(a,*b), c = 1,2,3,3,4,5,6,7 # a = 1, b = [2, 3, 3, 4, 5, 6], c = 7
*(a,*b), (*c,) = 1,2,3,4,5,'XY' # a = 1, b = [2, 3, 4, 5], c = ['X', 'Y']
*(a,*b), c, d = 1,2,3,3,4,5,6,7 # a = 1, b = [2, 3, 3, 4, 5], c = 6, d = 7
*(a,*b), (c, d) = 1,2,3,3,4,5,6,7 # ERROR -- 'int' object is not iterable
*(a,*b), (*c, d) = 1,2,3,3,4,5,6,7 # ERROR -- 'int' object is not iterable
*(a,*b), *(c, d) = 1,2,3,3,4,5,6,7 # ERROR -- two starred expressions in assignment
*(a,b), c = 'XY', 3 # ERROR -- need more than 1 value to unpack
*(*a,b), c = 'XY', 3 # a = [], b = 'XY', c = 3
(a,b), c = 'XY', 3 # a = 'X', b = 'Y', c = 3
*(a,b), c = 'XY', 3, 4 # a = 'XY', b = 3, c = 4
*(*a,b), c = 'XY', 3, 4 # a = ['XY'], b = 3, c = 4
(a,b), c = 'XY', 3, 4 # ERROR -- too many values to unpack
如何手动正确推导出此类表达式的结果? 最佳答案
对于这篇文章的长度,我深表歉意,但我决定选择完整性。
一旦你知道了一些基本规则,就不难概括它们。我会尽力用几个例子来解释。由于您正在谈论“手动”评估这些,我将建议一些简单的替换规则。基本上,如果所有可迭代对象的格式都相同,您可能会发现更容易理解表达式。
仅出于解包的目的,以下替换在 =
的右侧有效(即对于右值):
'XY' -> ('X', 'Y')
['X', 'Y'] -> ('X', 'Y')
如果您发现一个值没有被解包,那么您将撤消替换。 (有关进一步说明,请参见下文。)
此外,当您看到“裸”逗号时,请假装有一个顶级元组。在左侧和右侧执行此操作(即对于左值和右值):
'X', 'Y' -> ('X', 'Y')
a, b -> (a, b)
考虑到这些简单的规则,以下是一些示例:
(a,b), c = "XY", "Z" # a = 'X', b = 'Y', c = 'Z'
应用上述规则,我们将
"XY"
转换为 ('X', 'Y')
,并覆盖括号中的裸逗号:((a, b), c) = (('X', 'Y'), 'Z')
这里的视觉对应使得分配的工作方式相当明显。
这是一个错误的例子:
(a,b), c = "XYZ"
遵循上述替换规则,我们得到以下结果:
((a, b), c) = ('X', 'Y', 'Z')
这显然是错误的;嵌套结构不匹配。现在让我们看一个稍微复杂的例子它是如何工作的:
(a,b), c, = [1,2],'this' # a = '1', b = '2', c = 'this'
应用上述规则,我们得到
((a, b), c) = ((1, 2), ('t', 'h', 'i', 's'))
但是现在从结构中可以清楚地看出
'this'
不会被解包,而是直接分配给 c
。所以我们撤消了替换。((a, b), c) = ((1, 2), 'this')
现在让我们看看当我们将
c
包装在一个元组中时会发生什么:(a,b), (c,) = [1,2],'this' # ERROR -- too many values to unpack
成为
((a, b), (c,)) = ((1, 2), ('t', 'h', 'i', 's'))
同样,错误是显而易见的。
c
不再是裸变量,而是序列内部的变量,因此右侧对应的序列被解包为 (c,)
。但是序列有不同的长度,所以有一个错误。现在使用
*
运算符进行扩展解包。这有点复杂,但它仍然相当简单。以 *
开头的变量成为一个列表,其中包含相应序列中未分配给变量名称的所有项目。从一个相当简单的例子开始:a, *b, c = "X...Y" # a = 'X', b = ['.','.','.'], c = 'Y'
这变成
(a, *b, c) = ('X', '.', '.', '.', 'Y')
分析这一点的最简单方法是从两端开始工作。
'X'
分配给 a
, 'Y'
分配给 c
。序列中的其余值放在一个列表中并分配给 b
。像
(*a, b)
和 (a, *b)
这样的左值只是上述情况的特例。一个左值序列中不能有两个 *
运算符,因为它会产生歧义。像这样的值 (a, *b, *c, d)
- 在 b
或 c
中的值会在哪里?稍后我将考虑嵌套的情况。*a = 1 # ERROR -- target must be in a list or tuple
这里的错误是不言自明的。目标 (
*a
) 必须在元组中。*a, = (1,2) # a = [1,2]
这是有效的,因为有一个裸逗号。应用规则...
(*a,) = (1, 2)
由于除了
*a
之外没有其他变量,因此 *a
包含了右值序列中的所有值。如果用单个值替换 (1, 2)
会怎样?*a, = 1 # ERROR -- 'int' object is not iterable
变成
(*a,) = 1
同样,这里的错误是不言自明的。你不能解压一些不是序列的东西,
*a
需要解压一些东西。所以我们把它放在一个序列中*a, = [1] # a = [1]
相当于
(*a,) = (1,)
最后,这是一个常见的混淆点:
(1)
与 1
相同——您需要一个逗号来区分元组和算术语句。*a, = (1) # ERROR -- 'int' object is not
现在用于嵌套。实际上这个例子不在你的“NESTED”部分;也许你没有意识到它是嵌套的?
(a,b), *c = 'XY', 2, 3 # a = 'X', b = 'Y', c = [2,3]
成为
((a, b), *c) = (('X', 'Y'), 2, 3)
顶级元组中的第一个值被分配,顶级元组中的其余值(
2
和 3
)被分配给 c
—— 正如我们所期望的那样。(a,b),c = 1,2,3 # ERROR -- too many values to unpack
*(a,b), c = 1,2,3 # a = 1, b = 2, c = 3
我已经在上面解释了为什么第一行会抛出错误。第二行很愚蠢,但这就是它起作用的原因:
(*(a, b), c) = (1, 2, 3)
如前所述,我们从头开始工作。
3
分配给 c
,然后剩余的值分配给前面带有 *
的变量,在本例中为 (a, b)
。所以这等价于 (a, b) = (1, 2)
,这恰好有效,因为有正确数量的元素。我想不出任何原因会出现在工作代码中。相似地,*(a, *b), c = 'this' # a = 't', b = ['h', 'i'], c = 's'
变成
(*(a, *b), c) = ('t', 'h', 'i', 's')
从两端开始工作,
's'
分配给 c
, ('t', 'h', 'i')
分配给 (a, *b)
。从末端再次工作, 't'
被分配给 a
, ('h', 'i')
作为列表分配给 b 。这是另一个不应出现在工作代码中的愚蠢示例。关于python - 解包、扩展解包和嵌套扩展解包,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6967632/