In this article, we will go through unpacking
, extended unpacking
& nested unpacking
. Let’s get started.
在本文中,我们将介绍unpacking
, extended unpacking
和nested unpacking
。 让我们开始吧。
开箱 (Unpacking)
The process of splitting the packed values into individual elements is called ‘unpacking
’. The packed values are strings, lists, tuples, sets, and dictionaries
.
将打包值拆分为单个元素的过程称为“ unpacking
”。 打包的值是strings, lists, tuples, sets, and dictionaries
。
During the unpacking elements from RHS
(Right Hand Side) are split into its relative position on the LHS
(Left Hand Side). We will see how this works in the following examples.
在拆箱过程中,来自RHS
(右手侧)的元件被分成其在LHS
(左手侧)上的相对位置。 在以下示例中,我们将了解其工作原理。
As we can see from the below two examples, elements from RHS
are unpacked into the variables on LHS
based on the relative position.
从下面的两个示例中可以看到, RHS
元素基于相对位置被解压缩到LHS
的变量中。
示例1:使用列表,元组和字符串 (Example 1: Using list, tuple & string)
示例2:使用集合和字典(Example 2: Using sets and dictionaries)
Since sets
& dictionaries
are an unordered collection of elements it is not guaranteed that unpacking will give the same results every time unlike strings, lists, and tuples which are an ordered collection of elements. Note that for dictionary
type, unpacking is done on keys.
由于sets
和dictionaries
是元素的无序集合,因此不能保证解压缩每次都会得到与元素有序集合的字符串,列表和元组不同的相同结果。 请注意,对于dictionary
类型,解压缩是在键上完成的。
Tips: Since sets & dictionaries are an unordered collection of elements, it is not recommended to use them for unpacking as there is no guarantee on the order of the results.
提示:由于集合和字典是元素的无序集合,因此不建议将它们用于解包,因为不能保证结果的顺序。
扩展的可重复拆包 (Extended iterable unpacking)
The extended iterable unpacking is done via the operators * and **.
扩展的可迭代拆包是通过运算符*和**完成的。
*运算符 (* Operator)
Sometimes we don’t want to unpack all the elements. We might be interested in unpacking only the first element or the last element and put rest of the elements into a variable. This can be achieved with the help of * operator.
有时我们不想拆开所有元素。 我们可能只对第一个元素或最后一个元素解压缩,然后将其余元素放入变量中感兴趣。 这可以通过*运算符来实现。
Note: * operator can only be used once on the LHS of assignment operator unless it is used in nested unpacking.
注意:*运算符只能在赋值运算符的LHS上使用一次,除非它在嵌套拆包中使用。
Example 3: Using list, tuple, string
示例3:使用列表,元组,字符串
Example 4: Using sets and dictionaries
示例4:使用集合和字典
**运算子(** Operator)
As you have noticed from the above examples, * operator iterates through the keys in the dictionary. If we would like to iterate through key-value
pairs, then we must use ** operator
.
从上面的示例中您已经注意到,*运算符遍历字典中的键。 如果要遍历key-value
对,则必须使用** operator
。
Note: ** operator can not be used on the LHS of assignment operator.
注意:**运算符不能在赋值运算符的LHS上使用。
Example 5: Using dictionaries
示例5:使用字典
嵌套拆包(Nested unpacking)
The same concepts we just went through for unpacking and extended unpacking applies to nested unpacking as well. The only difference is as the name suggests — it’s nested. Let’s look at the examples:
我们刚刚进行拆包和扩展拆包时所经历的相同概念也适用于嵌套拆包。 唯一的区别是顾名思义-它是嵌套的。 让我们看一下示例:
Example 6: Using list, tuple & string
示例6:使用列表,元组和字符串
结论(Conclusion)
Hope you have understood what is unpacking in Python and how it works.
希望您已经了解了Python中的解压缩及其工作方式。
If you are interested in learning more about Python, here are a few more articles which you might like:
如果您有兴趣了解有关Python的更多信息,请阅读以下几篇文章:
Thank you for reading this article. You can reach me at https://www.linkedin.com/in/chetanambi/
感谢您阅读本文。 您可以通过https://www.linkedin.com/in/chetanambi/与我联系
翻译自: https://medium.com/towards-artificial-intelligence/what-is-unpacking-in-python-5bd22954ce4d