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

Messages in english

Modérateur : xcasadmin

compsystems
Messages : 561
Inscription : sam. févr. 04, 2017 11:34 pm
Localisation : Colombia
Contact :

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

Message par compsystems » ven. oct. 25, 2019 10:18 pm

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]

parisse
Messages : 5739
Inscription : mar. déc. 20, 2005 4:02 pm
Contact :

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

Message par parisse » sam. oct. 26, 2019 11:32 am

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]] )

compsystems
Messages : 561
Inscription : sam. févr. 04, 2017 11:34 pm
Localisation : Colombia
Contact :

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

Message par compsystems » sam. oct. 26, 2019 7:11 pm

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
> [ ]

parisse
Messages : 5739
Inscription : mar. déc. 20, 2005 4:02 pm
Contact :

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

Message par parisse » lun. oct. 28, 2019 8:54 pm

why do you think there is an error?

Répondre