Search code examples
pythonlistloopsbinarypython-itertools

Converting binary lists to values required for combinations based on index of list


I generated a list of binary numbers in python with itertools, of which I want to convert all the 1 to 'ALL' and all the 0 to correspond to the index of the attribs list, where the attribs list is [1, 2], with the measure value 10 appended at the end of each list.

In essence, the list of binary numbers is

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

I want to convert them to

[(1, 2), (1, 'ALL'), ('ALL', 2), ('ALL', 'ALL')]

So that these will be a form of combinations for the list [1,2].

The final lists when printed should look like these:

[1, 2, 10]
[1, 'ALL', 10]
['ALL', 2, 10]
['ALL', 'ALL', 10]

However, I'm currently getting the following:

[2, 2, 10]
[2, 'ALL', 10]
['ALL', 2, 10]
['ALL', 'ALL', 10]

What am I doing wrong?

import itertools as it

measure = 10
attribs = [1, 2]

# generate binary table based on number of columns
outs = [i for i in it.product(range(2), repeat=(len(attribs)))]
print(outs)

for line in outs:
    line = list(line)

    # replace binary of 1 or 0 with 'ALL' or value from input
    for index, item in enumerate(line):
        print("index of line: " + str(index) + " with value: " + str(item))

        if (item == 1):
            line[index] = 'ALL'
        elif (item == 0):
            for i in range(len(attribs)):
                print("assigning element at line index " + str(index) + " to index of attribs: " + str(i) + " with value: " + str(attribs[i]))
                line[index] = attribs[i]

    line.append(measure)
    print(line)

Solution

  • In your elif part your loop will executed till the end that will always assign attribs[1] to line[index] which is 2.

        elif (item == 0):
            for i in range(len(attribs)):
                print("assigning element at line index " + str(index) + " to index of attribs: " + str(i) + " with value: " + str(attribs[i]))
                line[index] = attribs[i]
    

    Instead you need to keep track of the index of 0 and 1 so that you can do:

        elif (item == 0):
            print("assigning element at line index " + str(index) + " to index of attribs: " + str(i) + " with value: " + str(attribs[i]))
            line[index] = attribs[bin_index] 
    

    But after all, as a more pythonic way you can just use a nested list comprehension:

    In [46]: [['ALL' if i else ind for ind, i in enumerate(items, 1)] + [10] for items in lst]
    Out[46]: [[1, 2, 10], [1, 'ALL', 10], ['ALL', 2, 10], ['ALL', 'ALL', 10]]