Page 1 sur 1

more arguments in comprehension lists [ for ... for ]

Publié : ven. oct. 25, 2019 10:18 pm
par compsystems
Hi BP, in the future there will be support for more argument in comprehension lists [ for ... for ]

Code : Tout sélectionner

def double(x):
  return x*2 
[double(x) for x in range(10)] [enter] returns
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

[double(x) for x in range(10) if x mod 2==0] [enter] returns
[0, 4, 8, 12, 16]


[x+y for x in [10,30,50] for y in [20,40,60]] [enter] Syntax Error
expected [30, 50, 70, 50, 70, 90, 70, 90, 110]

Re: more arguments in comprehension lists [ for ... for ]

Publié : sam. oct. 26, 2019 11:32 am
par parisse
I can't, the parser does not accept this notation. Instead you can run
flatten([ [x+y for x in [10,30,50] ] for y in [20,40,60]] )

Re: more arguments in comprehension lists [ for ... for ]

Publié : sam. oct. 26, 2019 7:11 pm
par compsystems
Hello

Trying the following seems to be a error

purge(a,b,y);

Code : Tout sélectionner

list_a := [1, 2, three, y];
list_b := [2, 3, -4, y];
common_num := [];
for a in list_a:
  for b in list_b:
    if a == b:
      common_num.append(a)
common_num
> [2,y]

but

Code : Tout sélectionner

list_a := [1, 2, three, y];
list_b := [2, 3, -4, y];
common_num := mat2list([ [a for a in list_a] for b in list_b if a == b ]); 
common_num
> [ ]

Re: more arguments in comprehension lists [ for ... for ]

Publié : lun. oct. 28, 2019 8:54 pm
par parisse
why do you think there is an error?