Is it possible to get multiple return values from inline for? eg,:
a,b=[(1,2) for _ in range(3)]
such that:
a=[1,1,1]
b=[2,2,2]
You can use the zip function
>>> a, b = zip(*[(1,2) for _ in range(3)])
>>> a
(1, 1, 1)
>>> b
(2, 2, 2)
or also
>>> a, b = [1]*3, [2]*3
>>> a
[1, 1, 1]
>>> b
[2, 2, 2]
>>>
The zip
built-in can be kind of "abused" to yield this kind of output:
In [203]: a, b = zip(*((1,2) for _ in range(3)))
In [204]: a
Out[204]: (1, 1, 1)
In [205]: b
Out[205]: (2, 2, 2)
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.