Home > front end >  AND operator between multiple values in a function
AND operator between multiple values in a function

Time:12-06

I have a function that takes in two list values and returns the AND operator output of them. Now I need to make the function take multiple values and get the output. I have done this for now for two lists of flag values:

def and_op(lst1, lst2):
    return np.array([(lst1 & lst2) for lst1,lst2 in zip(lst1, lst2)])

and_op([0,0,1,1,0], [1,0,1,1,1])

OUTPUT:

array([0,0,1,1,0])

I now need to change this function such that the arguments are dynamically given and the number of arguments can be more than two.

and_op([0,1,1,0], [1,1,0,1], [1,1,1,0], [0,1,0,1])

How can I change the function so I can get output for the case above? I thought of *args, but got super confused on how to use & operator on it.

CodePudding user response:

Use all function and iterate your args.

def and_op(*args):
    return np.array([int(all([arg[i] for arg in args])) for i in range(len(args[0]))])
  • Related