0

I have a list with 4 elements. Each element is a correct score that I am pulling from a form. For example:

scoreFixed_1 = 1
scoreFixed_2 = 2
scoreFixed_3 = 3
scoreFixed_4 = 4

scoreFixed = [scoreFixed_1, scoreFixed_2, scoreFixed_3, scoreFixed_4]

Then, I need to add:

scoreFixed_1 to fixture[0][0]
scoreFixed_2 to fixture[0][1]
scoreFixed_3 to fixture[1][0]
scoreFixed_4 to fixture[1][1]

Hence, I need to create a triple for loop that outputs the following sequence so I can index to achieve the result above:

0 0 0
1 0 1
2 1 0
3 1 1

I have tried to use this to create this matrix, however I am only able to get the first column correct. Can anyone help?

for x in range(1):
    for y in range(1):
        for z in range(4):
            print(z, x, y)

which outputs:

0 0 0
1 0 0
2 0 0
3 0 0
8
  • There must be some logic behind this matrix that can be translated into a loop. Could you please add more information about how this matrix is formed? Commented Dec 2, 2021 at 16:08
  • are there any rules/conditions for what numbers go where? Commented Dec 2, 2021 at 16:08
  • As long as its in the given order that is written in the first code snippet, then it works for me Commented Dec 2, 2021 at 16:09
  • But how are those numbers generated? Since there' not ascending, a regular loop won't work. There must be some kinda logic behind it. Commented Dec 2, 2021 at 16:11
  • At the moment, there is no logic behind it. I simply need this to index in the correct order for another program. The first list has 4 elements, the second list has 2 elements and so does the third list. Commented Dec 2, 2021 at 16:12

3 Answers 3

2

Your logic does not generate the table, you want something like:

rownum = 0
for x in range(2):
    for y in range(2):
        print (rownum, x, y)
        rownum += 1

(Edit: The question has been changed, to accomplish the new desire, you want something like this:)

scoreIndex = 0
for x in range(2):
    for y in range(2):
        fixture[x][y] += scoreFixed[scoreIndex]
        scoreIndex += 1
Sign up to request clarification or add additional context in comments.

2 Comments

I think you forgot to increment scoreIndex
@ErikMcKelvey I sure did! Thanks... (Edited to correct)
0

After your edit, it seems like we can split the 'sequence' into:

  1. First column, regular ascending variable ( n += 1)
  2. Second and third column, binary counter (00, 01, 10, 11)
0 0 0
1 0 1
2 1 0
3 1 1

  ^ ^------- These seem like a binary counter 
                (00, 01, 10, 11)

^------ A regular ascending variable
                ( n += 1 )

Using that 'logic' we can create a code that looks like

import itertools 

scoreFixed = 0
for i in itertools.product([0,1],repeat=2): 
    print(scoreFixed, ' '.join(map(str,i)))
    scoreFixed += 1

And wil output:

0 0 0
1 0 1
2 1 0
3 1 1

As you can test in this online demo

Comments

-1
for x in range(4):
    z = int(bin(x)[-1])
    y = bin(x)[-2]
    y = int(y) if y.isdigit() else 0
    print(x, y, z)

1 Comment

Logic is simple. After the number, the next columns represent its binary values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.