python - Is there a way to output multiple lists from a single list comprehension expression? -


something like

x, y = [expression d in data] 

basically i'd obtain equivalent of this:

x = [] y = [] d in data:     x.append(d[0])     y.append(d[1]) 

where data nested list?

and if data list of dictionaries?

x = [] y = [] d in data:     x.append(d['key1'])     y.append(d['key2']) 

and if want apply different function each column data list of dictionaries?

x = [] y = [] d in data:     x.append(func1(d['key1']))     y.append(func2(d['key2'])) 

x, y = zip(*[d[:2] d in data]) 

i think want ... give list of x's , list of y's

if each row in data has d[0] , d[1] can do

x1,x2,x3 = 1,2,3 y1,y2,y3 = 3,4,5 data = [(x1,y1),(x1,y2),(x3,y3)] x,y = zip(*data) 

if have dict

from operator import itemgetter x,y,z = zip(*map(itemgetter('key1','key2','key3'),data)) 

if wanted apply function need

x,y = zip(*[(function1(row['key']),function2(row['key2'])) row in data]) 

Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -