本文介绍了如何解压序列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我不能这样做:

d = [x for x in range(7)]
a, b, c, d, e, f, g = *d

在哪里可以打开包装?仅在函数的括号之间?

Where is it possible to unpack?Only between parentheses of a function?

推荐答案

您正在使用 Extended Iterable Unpacking 用错误的方式.

You're using Extended Iterable Unpacking in wrong way.

d = [x for x in range(7)]
a, b, c, d, e, f, g = d
print(a, b, c, d, e, f, g)

否,

*提议对可迭代的解包语法进行更改,从而允许指定包罗万象"的名称,该名称将被分配所有未分配给常规"名称的项目的列表.

* proposes a change to iterable unpacking syntax, allowing to specify a "catch-all" name which will be assigned a list of all items not assigned to a "regular" name.

您可以尝试以下操作:

a, *params = d
print(params)

输出

[1, 2, 3, 4, 5, 6]

通常在需要将参数传递给函数时使用*(扩展的可迭代拆包)运算符.

Usually * (Extended Iterable Unpacking) operator is used when you need to pass parameters to a function.

注意

等效于可扩展迭代拆包 operator的Java脚本称为传播语法.

Javascript equivalent of Extended Iterable Unpacking operator is called spread syntax.

d = [...Array(7).keys()]
console.log(d)

var [a, ...b] = d
console.log(a,b)

这篇关于如何解压序列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 00:31