0

The problem I'm facing is that I have a list of lists.

a = [[1, 1, 1, 0, 0, 0],
    [1, 1, 1, 0, 1, 1], 
    [1, 0, 0, 1, 0, 0]]

I want to do AND/OR/Majority-voting operations on the elements of the lists inside the list a. I want to get a single list result by, for example, doing an AND operation index by index so it see if elements on index 0 (in the lists) fulfill AND operation criteria so it append 1 to a result list incase all elements on index 0 are 1, else 0.

and_operation_on_a = [1, 0, 0, 0, 0, 0]
def and_op(alist):
    
    result_list = []
        
    for x, y, z in zip(alist[0], alist[1], alist[2]):
        if x == 1 and y == 1 and z == 1:
            result_list.append(1)
        else:
            result_list.append(0)

    
    return result_list

I tried something like this as the list has 3 lists inside and it works for me but what I want to do is to generalize it. For example, for the function to do an AND/OR/Majority-voting operation (element-by-element or index-by-index) if given a variable number of lists inside. I shall be really thankful to you If someone can help me. I am stuck badly.

  • First you need to generalize passing multiple lists to zip. Then you need to generalize checking if something is true for all items in a collection. – mkrieger1 Oct 25 '22 at 20:25
  • Part 1: https://stackoverflow.com/questions/5938786/how-would-you-zip-an-unknown-number-of-lists-in-python – mkrieger1 Oct 25 '22 at 20:27
  • 1
    I mean you could use numpy `np.all(a, axis=0)` Not sure if you were trying to write it the long way, which is cool too. – Chris Oct 25 '22 at 20:27
  • Part 2: https://stackoverflow.com/questions/19389490/how-do-pythons-any-and-all-functions-work – mkrieger1 Oct 25 '22 at 20:30
  • Thank you very much for answering. I really appreciate. But I am new (learning) and not experienced with coding so can't code very efficiently. I would request if you can elaborate a bit more so it would get a bit easy for me to understand. – Shehzad Khan Oct 25 '22 at 20:44

1 Answers1

0

Here's one way to generalize your code. This script is written so that in the event of a tie, the result for maj_op defaults to zero.

a = [[1, 1, 1, 0, 0, 0],
    [1, 1, 1, 0, 1, 1], 
    [1, 0, 0, 1, 0, 0]]

def and_op(alist):
    return_list = []
    for tup in zip(*alist):
        return_list.append(int(all(tup)))
    return return_list

def or_op(alist):
    return_list = []
    for tup in zip(*alist):
        return_list.append(int(any(tup)))
    return return_list

def maj_op(alist):
    return_list = []
    half = len(alist)//2
    for tup in zip(*alist):
        return_list.append(int(sum(tup)>half))
    return return_list

print(and_op(a))
print(or_op(a))
print(maj_op(a))

The result:

[1, 0, 0, 0, 0, 0]
[1, 1, 1, 1, 1, 1]
[1, 1, 1, 0, 0, 0]

Alternatively, this can all be shortened with list comprehension. In particular, the rewritten functions

def and_op(alist):
    return [int(all(tup)) for tup in zip(*alist)]

def or_op(alist):
    return [int(any(tup)) for tup in zip(*alist)]

def maj_op(alist):
    return [int(sum(tup)>len(alist)//2) for tup in zip(*alist)]

yield exactly the same results.


Regarding your comment:

def maj_op_2(a,b):
    return [int(sum(x*w for x,w in zip(col,b))>=0) for col in zip(*a)]
Ben Grossmann
  • 4,387
  • 1
  • 12
  • 16
  • I have another question if you can answer. For example if we have -1 instead of 0. E.g., a = [[1, 1, 1, -1, -1, -1], [1, 1, 1, -1, 1, 1], [1, -1, -1, 1, -1, -1]] and another list, eg., b = [0.7, 0.5, 0.4] For majority voting, How would we multiply the first item in b (0.7) with first list in a, second item in b with second list in a and third item of b with third list of a? And then do the sum operation on the resultant list of list and append 1 if the sum is greater than 1 or 0 if result is less than 0? – Shehzad Khan Nov 07 '22 at 12:58
  • @ShehzadKhan First of all, if my answer addressed your question to your satisfaction, please "accept" it by clicking the check mark (✓) next to its vote arrows – Ben Grossmann Nov 07 '22 at 13:21
  • I accepted the answer. Sorry for doing it late. I am learning so, it is requested if you can answer the following question, that would be really helpful and I shall be thankful. – Shehzad Khan Nov 07 '22 at 13:26
  • @ShehzadKhan To your comment, I've edited my answer to give you a list comprehension approach. You never said what to do in the event of a sum exactly equal to zero, though, so I just assumed that ties go to `0` for my convenience. – Ben Grossmann Nov 07 '22 at 13:29
  • @ShehzadKhan You never commented on my post with any questions, so I'm not sure if there's anything about my post that you're trying to understand but haven't figured out. That said, I think it is probably important for you to understand that the way that I am avoiding if/else statements is by taking advantage of Python's type casting. A conditional expression (such as `sum(...)>0` is either `True` or `False`. Calling `int(...)` on that expression yields `1` if the input is true or `0` if the input is false. – Ben Grossmann Nov 07 '22 at 13:35
  • @ShehzadKhan You also never said whether you can use [numpy](https://numpy.org/) in this context. Depending on what you're using these methods for (and particularly if you're concerned with speed), you might want to consider changing these methods so that they use numpy. – Ben Grossmann Nov 07 '22 at 13:38
  • Thank you very much for your reply. and I am sorry for something that I missed. In case the sum is equal to zero, it should append 1. If sum is 0 or positive, append 1. Else 0. Your post was 100% clear. The question was about to make the changes in case of a second list and multiply then sum, as I described above. I don't have any preferences. Whatever library or in whatever way it is possible to do the task, I am happy about it because I need to get the task done. I don't really care about speed at the moment but Speed would be nice if possible to make it fast. – Shehzad Khan Nov 07 '22 at 13:55
  • @ShehzadKhan I changed the code to address the zero case. Did you see the function that I added to the end of my answer? – Ben Grossmann Nov 07 '22 at 13:57
  • Yes I saw it and checked. Its working perfect. Thanks a lot sir. – Shehzad Khan Nov 07 '22 at 14:04