Why can't I do this:
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?
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)
Where it's possible to unpack? Only between parentheses of a function?
No,
*
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.
You can try something like this:
a, *params = d
print(params)
Output
[1, 2, 3, 4, 5, 6]
Usually *
( Extended Iterable Unpacking ) operator is used when you need to pass parameters to a function.
Note
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)
You can also use this:
>>> a,b,c,d,e,f,g = range(7)
>>> a
0
>>> b
1
>>> c
2
You don't appear to need the *
>>> z = [x for x in range(7)]
>>> a,b,c,d,e,f,g = z
>>> a
0
>>> b
1
>>> c
2
>>>
(I've used z rather than d twice.)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.