Home > front end >  Combining "True" values in multiple list into a single list, (index sensitve)
Combining "True" values in multiple list into a single list, (index sensitve)

Time:12-22

Let's suppose I have two lists:

l1 = np.zeros(5,bool)
l2 = np.zeros (5,bool)

l1[3] = True 
l2[1] = True 

Output:

[False False False  True False]
[False  True False False False]

And based on these lists I want a single list which has indexes set to true based on the index of Trues (one or more) in a number of lists. All the lists have the same length and the target list must have the same length as well. What could be the pythonic way to do that so that I may get the desired list:

List 3: Desired Output:

[False True False  True False]

CodePudding user response:

If what I understand is that you want to apply an or operator between vectors, this would be the way to develop it:

import numpy as np

l1 = np.zeros(5,bool)
l2 = np.zeros (5,bool)

l1[3] = True 
l2[1] = True 

l_full = np.logical_or(l1,l2)
l_full
# array([False,  True, False,  True, False])

CodePudding user response:

It's as simple as writing

l1 | l2

CodePudding user response:

Similar to what @kosciej answered.

print(l1 | l2)

CodePudding user response:

Yet another way:

out = l1 l2

Output:

array([False,  True, False,  True, False])
  • Related