Home > Enterprise >  For loop unintentionally skips over item when trying to filter a list using items from another list
For loop unintentionally skips over item when trying to filter a list using items from another list

Time:10-17

I am working on finding all possible moves on a chessboard for a given position. I am trying to filter out all positions a piece is on to make a 2d array of the board with just the open positions. However, while filtering each row of the array it skips over every other item.

Code:

files = ['A','B','C','D','E','F','G','H']

ranks_n = [1,2,3,4,5,6,7,8]
ranks = ['1','2','3','4','5','6','7','8']

board = [
    ['A8','B8','C8','D8','E8','F8','G8','H8'],
    ['A7','B7','C7','D7','E7','F7','G7','H7'],
    ['A6','B6','C6','D6','E6','F6','G6','H6'],
    ['A5','B5','C5','D5','E5','F5','G5','H5'],
    ['A4','B4','C4','D4','E4','F4','G4','H4'],
    ['A3','B3','C3','D3','E3','F3','G3','H3'],
    ['A2','B2','C2','D2','E2','F2','G2','H2'],
    ['A1','B1','C1','D1','E1','F1','G1','H1'],
]

board_n = [
    [18,28,38,48,58,68,78,88],
    [17,27,37,47,57,67,77,87],
    [16,26,36,46,56,66,76,86],
    [15,25,35,45,55,65,75,85],
    [14,24,34,44,54,64,74,84],
    [13,23,33,43,53,63,73,83],
    [12,22,32,42,52,62,72,82],
    [11,21,31,41,51,61,71,81]
]
white = {
    'p1': 'A2',
    'p2': 'B2',
    'p3': 'C2',
    'p4': 'D2',
    'p5': 'E2',
    'p6': 'F2',
    'p7': 'G2',
    'p8': 'H2',
    'R1': 'A1',
    'N1': 'B1',
    'B1': 'C1',
    'Q': 'D1',
    'K': 'E1',
    'B2': 'F1',
    'N2': 'G1',
    'R2': 'H1',
}

black = {
    'p1': 'A7',
    'p2': 'B7',
    'p3': 'C7',
    'p4': 'D7',
    'p5': 'E7',
    'p6': 'F7',
    'p7': 'G7',
    'p8': 'H7',
    'R1': 'A8',
    'N1': 'B8',
    'B1': 'C8',
    'Q': 'D8',
    'K': 'E8',
    'B2': 'F8',
    'N2': 'G8',
    'R2': 'H8',
}

b_keys=list(black.keys())
b_values=list(black.values())

w_keys=list(white.keys())
w_values=list(white.values())

pos_board = []
board_file = []
for b in board:
  board_file = b
  for n in b:
    if n in w_values or n in b_values:
      print(n)
      board_file.remove(n)
  pos_board.append(board_file)

for p in pos_board:
  print(p)

Output:

C8
E8
G8
A7
C7
E7
G7
A2
C2
E2
G2
A1
C1
E1
G1
['B8', 'D8', 'F8', 'H8']
['B7', 'D7', 'F7', 'H7']
['A6', 'B6', 'C6', 'D6', 'E6', 'F6', 'G6', 'H6']
['A5', 'B5', 'C5', 'D5', 'E5', 'F5', 'G5', 'H5']
['A4', 'B4', 'C4', 'D4', 'E4', 'F4', 'G4', 'H4']
['A3', 'B3', 'C3', 'D3', 'E3', 'F3', 'G3', 'H3']
['B2', 'D2', 'F2', 'H2']
['B1', 'D1', 'F1', 'H1']

The 1st, 2nd, 7th, and 8th ranks should be completely empty because there are all pieces there but it shows that there are no pieces on files B, D, F, and H for those ranks.

CodePudding user response:

You probably want this one. I replaced the board_file with board_row and put the initialization inside the loop.

b_keys=list(black.keys())
b_values=list(black.values())

w_keys=list(white.keys())
w_values=list(white.values())

pos_board = []
for b in board:
  board_row = []
  for n in b:
    if n not in w_values   b_values:
        board_row.append(n)
  pos_board.append(board_row)

for p in pos_board:
  print(p)

CodePudding user response:

board_file is actually just an alias for b. So when you remove n from board_file, you are also removing it from b. To prevent this, you can make a deepcopy of b

from copy import deepcopy

pos_board = []
board_file = []
for b in board:
  board_file = deepcopy(b)
  for n in b:
    if n in w_values or n in b_values:
      print(n)
      board_file.remove(n)
  pos_board.append(board_file)

for p in pos_board:
  print(p)
  • Related