I have an array which looks like this
[1, 0, 1 , 0 , 0, 1]
And I want to get those indexes that have 1 in it.
So here I would get an array of [0, 2 , 5]
and then based on it I would create a new array that takes these numbers and exponantiate 2 with them
So the end array is
[2**0, 2**2, 2**5]
Is there a way to write it as shortly as possible?
you could use enumerate in a list comprehension:
a = [1, 0, 1 , 0 , 0, 1]
b = [2**idx for idx, v in enumerate(a) if v]
b
[1, 4, 32]
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