Return multiple lists from comprehension in python
Is there any way in Python to return multiple lists from a comprehension?
I want to do something to the effect of:
x,y = [i,(-1*j) for (i,j) in enumerate(range(10))]
# x = [0 .. 9]
# y = [0 .. -9]
that's a dumb example, but I'm just wondering if it's possible.
x,y =zip(* [(i,(-1*j)) for (i,j) in enumerate(range(10))] )
你只需解压缩列表
xy = [(1,2),(3,4),(5,6)]
x,y = zip(*xy)
# x = (1,3,5)
# y = (2,4,6)
You can skip the list comprehension:
>>> x,y=range(0,10), range(0,-10,-1)
>>> x
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> y
[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
Or you could do:
>>> x,y=map(list, zip(*[(e,-e) for e in range(10)]))
链接地址: http://www.djcxy.com/p/32514.html
上一篇: Android Studio:ExternalSystemException
下一篇: 从Python中的理解返回多个列表