1

I have the following exercise involving 2 matrices – the first being ‘Equipment available to four leagues’, the second being the ‘teams per league’ (i.e, two teams per league). Note, there is sometimes more ‘equipment available’ than there are teams in a particular league, which will leave some equipment un-used. The “0” is just a place holder for the lack of equipment or the lack of a team.

**Equipment**
equip =
      0     1    2
0   E_1   E_2    0
1   E_3   E_4  E_5
2   E_6   E_7  E_8
3   E_9   E_10   0

**Teams**
team =
      0     1
0   T_1   T_2 
1   T_3   T_4      
2   T_5   T_6   
3   T_7     0

I simply want to output a pairing of the 1st element of the equip matrix with the 1st element of the team matrix, and so on (pairings are within leagues, i.e. aligned by matching rows).


Note, in league 1 (i.e., the second row), there is an extra set of equipment, so E_5 is not paired. Thus, in league 2 (i.e., the next row), E_6 is paired with T_5. This dynamic is manifest in the next row as well. The pairing ends with the last Team – T_7 (which is paired with E_9).

Here is my code, which is repeating some output pairings (unwantingly), and eventually stopping with an ‘IndexError’:

### Initialize row and column ###
# Equipment row, column:
erow=0
ecol=0

# Teams row, column:
trow=0
tcol=0

for erow in range(len(equip)):
  for ecol in range(len(equip[0])):
    if ((equip[][] != “0”) and (tcol < 2)):
      print(equip[erow][ecol])
      print(team[trow][tcol]
      tcol = tcol + 1
      print()
      continue
    else:
      erow = erow + 1
      ecol=0
      print(equip[erow][ecol])
      trow = trow + 1
      tcol = 0
      print(team[trow][tcol])
      print()
      continue

Desired Output:
E_1
T_1

E_2
T_2

E_3
T_3

E_4
T_4

E_6
T_5

E_7
T_6

E_9
T_7 

My (undesirable) output:
E_1
T_1

E_2
T_2

E_3
T_3

E_3
T_3

E_4
T_4

E_6
T_5

E_6
T_5

E_7
T_6

E_9
T_7

E_9
T_7

E_10
0

IndexError: list index out of range

I believe the issue is that my incrementing based on the conditions is not carrying over as input for the next iteration of the loop. The loop is incrementing by row and column independently of my row+1 and col+1 statements. I have tried to use ‘break’ as well as ‘continue’ in different ways – but this has not solved the issue. As well, I have also tried nesting 4 ‘for statements’ – i.e., using both the equipment (row & col) and teams (row & col) matrices nested together, and this yields even messier results. I would greatly appreciate any input from python programmer gurus out there😊. Thank you!

1 Answer 1

0

There are some issues with your code like Syntax Errors, Incorrect Row/Column Update Logic etc. Here is the improved and revised code:

equip = [
    ["E_1", "E_2", "0"],
    ["E_3", "E_4", "E_5"],
    ["E_6", "E_7", "E_8"],
    ["E_9", "E_10", "0"]
]

team = [
    ["T_1", "T_2"],
    ["T_3", "T_4"],
    ["T_5", "T_6"],
    ["T_7", "0"]
]

for erow, trow in zip(equip, team):
    e_filtered = (e for e in erow if e != "0")
    t_filtered = (t for t in trow if t != "0")
    
    for e, t in zip(e_filtered, t_filtered):
        print(e)
        print(t)
        print()

Output:

E_1
T_1

E_2
T_2

E_3
T_3

E_4
T_4

E_6
T_5

E_7
T_6

E_9
T_7
Sign up to request clarification or add additional context in comments.

Comments

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.