简体   繁体   English

将字典键解包到带有星号的列表中

[英]unpaking dictionary keys to a list with asterisk

i tried to unpack dictionary keys to a variable with asterisk,but it raise error.我试图用星号将字典键解压到一个变量中,但它引发了错误。 but if i put a comma after a variable, it will works well!但是如果我在变量后面放一个逗号,它会很好用! why this happen?为什么会这样?

test_2 = {'x': 20,
      'y': 60,
      'z': 100
      }
 *d = test_2      
                   
 print(d)

>>>SyntaxError: starred assignment target must be in a list or tuple

this one will works well:这个会很好用:

*d, = test_2      
                   
 print(d)               #['x', 'y', 'z']
 

It does not work because that is how the syntax was designed.它不起作用,因为这是设计语法的方式。 If you read the error, the assignment has to be into a list or tuple.如果您阅读错误,则分配必须在列表或元组中。

You assigned into a variable.你赋值给一个变量。


test = {'a':1, 'b':2, 'c':3, 'd':4}

z, *y, x = test

y will take on the value of everything that is not the first and last items. y 将承担所有不是第一个和最后一个项目的价值。

There are other ways of unpacking dictionary keys, python has many methods of doing this.还有其他解包字典键的方法,python 有很多方法可以做到这一点。

https://medium.com/swlh/how-to-pack-and-unpack-data-in-python-tuples-and-dictionaries-55d218c65674 this has a pretty good coverage of what you can do. https://medium.com/swlh/how-to-pack-and-unpack-data-in-python-tuples-and-dictionaries-55d218c65674这很好地涵盖了您可以做什么。

The regular case is to unpack into a list* of variables:常规情况是解压到变量列表*中:

a, b, c = some_iterable

* a tuple of variables to be pedantic * 迂腐的变量元组

The "special case" is to unpack into a list where there are fewer variables than items to unpack, and one variable in the list takes all remaining items: “特殊情况”是解包到一个列表中,其中变量少于要解包的项目,并且列表中的一个变量包含所有剩余项目:

a, b, *c = some_very_long_iterable

This assigns the first and second item to a and b respectively, and all the rest to c as a list.这将第一项和第二项分别分配给ab ,其余的都作为列表分配给c

Given this, what does *d = test_2 do…?鉴于此, *d = test_2做了什么……? It puts everything in test_2 into d .它将test_2所有test_2放入d Which is the same that d = test_2 would do.这与d = test_2会做的一样。 So the syntax *d = ... has no specific useful meaning, and is hence a syntax error.所以语法*d = ...没有特定的有用含义,因此是一个语法错误。

You may debate whether it should work or not, but the definition of an unpacking assignment is that the left-hand side needs to be a list of variables , one of which may have an asterisk.你可能会争论它是否应该工作,但解包赋值的定义是左侧需要是一个变量列表,其中一个可能有一个星号。 *d isn't a list of variables. *d不是变量列表。 *d, is . *d, The comma makes the tuple.逗号构成元组。

Instead of dictionary let's assume a list for simplicity:为简单起见,让我们假设一个列表,而不是字典:

test_2 = ['a', 'b', 'c']
*d = test_2
print(d)

Suppose the *d = test_2 works, then what would be the d ?假设*d = test_2有效,那么d会是什么? It's going to be : ['x', 'y', 'z'] .它将是: ['x', 'y', 'z'] Isn't it exactly same as d = test_2 ?是不是和d = test_2完全一样?

I guess they didn't allow this syntax because it's pointless to have single starred assignment in the left hand side.我猜他们不允许这种语法,因为在左侧有单星分配是没有意义的。 That's why you get SyntaxError: starred assignment target must be in a list or tuple .这就是为什么您会收到SyntaxError: starred assignment target must be in a list or tuple

When you put a comma after the *d , you create a "tuple" which is a reasonable case to have "starred assignment" (usual unpacking with/without other symbols inside it like a, b, *rest = ... ).当您在*d之后放置逗号时,您会创建一个“元组”,这是具有“带星号的分配”的合理情况(通常在其中使用/不使用其他符号进行解包,例如a, b, *rest = ... )。

*d, = test_2     # works
(*d,) = test_2   # works
[*d] = test_2    # works
*d = test_2      # doesn't work

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

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