在python中解包什么

In this article, we will go through unpacking, extended unpacking & nested unpacking. Let’s get started.

在本文中,我们将介绍unpackingextended unpackingnested 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.

由于setsdictionaries是元素的无序集合,因此不能保证解压缩每次都会得到与元素有序集合的字符串,列表和元组不同的相同结果。 请注意,对于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的更多信息,请阅读以下几篇文章:

  1. How to use Python for and while loops

    如何使用Python for和While循环

  2. Optimization in Python — Interning

    Python优化—实习

  3. Optimization in Python — Peephole

    Python优化—窥Kong

  4. Mutability and Immutability in Python

    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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值