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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With