编程语言
首页 > 编程语言> > python – 解包,扩展解包和嵌套扩展解包

python – 解包,扩展解包和嵌套扩展解包

作者:互联网

考虑一下这些表达方式……请耐心等待…这是一个很长的清单……

(注意:重复一些表达式 – 这只是为了呈现“上下文”)

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

你如何理解这种复杂性和混乱.在手动计算这些表达式的结果时,如何总是正确的.或者,在阅读别人的代码时,我应该忽略它们,而不是试图理解表达式实际上在做什么吗?

解决方法:

我对这篇文章的篇幅感到抱歉,但我决定选择完整性.

一旦你了解了一些基本规则,就不难概括它们.我会尽力用一些例子来解释.既然你正在谈论“手动”评估这些,我会建议一些简单的替换规则.基本上,如果所有迭代都以相同的方式格式化,您可能会发现理解表达式更容易.

仅出于解包的目的,以下替换在=的右侧有效(即对于rvalues):

'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’),并覆盖parens中的裸逗号:

((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)这样的左值只是上述的特例.在一个左值序列中不能有两个*运算符,因为它不明确.在b或c中,值会在何处(a,* b,* c,d)出现?我马上就会考虑嵌套的情况.

*a = 1                               # ERROR -- target must be in a list or tuple

这里的错误是相当不言自明的.目标(* a)必须是元组.

*a, = (1,2)                          # a = [1,2]

这是有效的,因为有一个裸露的逗号.应用规则……

(*a,) = (1, 2)

由于除了* a之外没有其他变量,* a会覆盖rvalue序列中的所有值.如果用单个值替换(1,2)怎么办?

*a, = 1                              # ERROR -- 'int' object is not iterable

(*a,) = 1

同样,这里的错误是不言自明的.你不能解压缩不是序列的东西,而*需要解压缩的东西.所以我们把它按顺序排列

*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作为列表.这是另一个不应该出现在工作代码中的愚蠢示例.

标签:iterable-unpacking,python,python-3-x,argument-unpacking
来源: https://codeday.me/bug/20190915/1804835.html