Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a if-else list comprehension in Julia

I am trying to use zip in a pythonic way but in Julia. Given two lists:

a =[2;3;4;5;6]
b =[0;7;8;9;10]

I would like to create the following list comprehension,

c = [x for (x,y) in zip(a, b) if (x<y) else y]

that returns c = [0;3;4;5;6]. Instead I get syntax: expected "]" returned.

like image 323
Marc Walton Avatar asked Nov 03 '19 15:11

Marc Walton


1 Answers

You have to rewrite your comprehension such that the condition is in the generator's "body":

c = [x < y ? x : y for (x, y) in zip(a, b)]

The if-condition in comprehensions is purely for filtering at the moment (although it might be possible to add the meaning you want).

like image 64
pfitzseb Avatar answered Oct 11 '22 23:10

pfitzseb