Home > Software engineering >  Appending elements from multiple lists into a new list with certain limitation
Appending elements from multiple lists into a new list with certain limitation

Time:06-23

This is kinda weird, but right now I'm having an issue with appending items from multiple lists with certain limitations. Here's an example...

I have a list, with 2 lists inside separately (basically a 2x2 dimensional list)

some_list: list[list[int]] = [[2, 5, 4, 8], [1, 9, 3, 7]]

So, the problem here is I don't know how to append 2 elements from each list inside the list separately and continuously... (I don't really know how to call this appending thing, but based on my problem and the codes can get you to solve it right away)

Well, I want to get the result from some_list into a new list, let's say new_list And here's what I want to achieve:

some_list: list[list[int]] = [[2, 5, 4, 8], [1, 9, 3, 7]]

new_list: list[list[int]] = [[2, 5, 1, 9], [4, 8, 3, 7]]

Anyway, from the code above, I want that 2 and 5 is in the first list, combined with the first and second elements from the next list, and that would be 1 and 9. Followed to the next list inside new_list, I want that the third and fourth elements from the first list on a list to be also combined with the next third and fourth elements from the second list on a list, and that would be 4 and 8 (from the first list) and combined with 3 and 7 (from the second list)

I hope that this explanation may help you to solve my problem, and this one is the real problem I got (9x9 dimensional list)

BOARD: list[list[str]] = [['-', '3', '4', '-', '-', '6', '-', '8', '-'],
                          ['5', '2', '7', '-', '1', '8', '6', '-', '3'],
                          ['-', '6', '-', '-', '-', '9', '4', '-', '2'],
                          ['-', '-', '5', '-', '4', '-', '-', '-', '-'],
                          ['3', '9', '-', '8', '-', '-', '5', '-', '-'],
                          ['-', '4', '-', '-', '-', '2', '9', '3', '6'],
                          ['1', '-', '-', '6', '8', '4', '-', '2', '-'],
                          ['2', '-', '-', '-', '-', '-', '8', '1', '-'],
                          ['-', '-', '-', '2', '7', '1', '3', '-', '-']]

I want to achieve the same as the examples I showed the above, simply to put it like this one... I want to get for the first list on my new list would be: n_board: [['-', '3', '4', '5', '2', '7', '-', '6', '-']]... This was only done for the first 0-3 row and column, but there's still a lot left, that I didn't be able to come up with a better solution... So basically, before ending this I wanna achieve from the BOARD list into something like this after done such operations:

Note: I do this manually, not using for/while loop at all...

new_board: list[list[str]] = [['-', '3', '4', '5', '2', '7', '-', '6', '-'], 
                              ['-', '-', '5', '3', '9', '-', '-', '4', '-'], 
                              ['1', '-', '-', '2', '-', '-', '-', '-', '-'], 
                              ['-', '-', '6', '-', '1', '8', '-', '-', '9'], 
                              ['-', '4', '-', '8', '-', '-', '-', '-', '2'], 
                              ['6', '8', '4', '-', '-', '-', '2', '7', '1'], 
                              ['-', '8', '-', '6', '-', '3', '4', '-', '2'], 
                              ['-', '-', '-', '5', '-', '-', '9', '3', '6'], 
                              ['-', '2', '-', '8', '1', '-', '3', '-', '-']]

I hope you guys will get the idea for the solution, and I'm sorry I can't explain this any further...

Please help me solve this problem, I really can't explain this any further because I also don't know how to come up with such an efficient solution... Thank you kindly :)

CodePudding user response:

It looks like you've proposed two versions of the same problem, so I'm just going to solve the second one, since you say it's the "real" problem.

Basically, the problem reduces to taking 3x3 subgrids of the board and flattening them out into 1-D lists. You can generate the top left corners of each of these subgrids, and then use a list comprehension to generate the rows:

from itertools import product
STRIDE = 3
starts = list(product(range(0, len(BOARD), STRIDE), repeat=2))

result = []
for row_s, col_s in starts:
    row = [
           BOARD[row][col] for row, col in product(
           range(row_s, row_s STRIDE), range(col_s, col_s   STRIDE)
          )]
    result.append(row)

print(result)

This outputs:

[
 ['-', '3', '4', '5', '2', '7', '-', '6', '-'],
 ['-', '-', '6', '-', '1', '8', '-', '-', '9'],
 ['-', '8', '-', '6', '-', '3', '4', '-', '2'],
 ['-', '-', '5', '3', '9', '-', '-', '4', '-'],
 ['-', '4', '-', '8', '-', '-', '-', '-', '2'],
 ['-', '-', '-', '5', '-', '-', '9', '3', '6'],
 ['1', '-', '-', '2', '-', '-', '-', '-', '-'],
 ['6', '8', '4', '-', '-', '-', '2', '7', '1'],
 ['-', '2', '-', '8', '1', '-', '3', '-', '-']
]

CodePudding user response:

some_lst = [['-', '3', '4', '-', '-', '6', '-', '8', '-'],
            ['5', '2', '7', '-', '1', '8', '6', '-', '3'],
            ['-', '6', '-', '-', '-', '9', '4', '-', '2'],
            ['-', '-', '5', '-', '4', '-', '-', '-', '-'],
            ['3', '9', '-', '8', '-', '-', '5', '-', '-'],
            ['-', '4', '-', '-', '-', '2', '9', '3', '6'],
            ['1', '-', '-', '6', '8', '4', '-', '2', '-'],
            ['2', '-', '-', '-', '-', '-', '8', '1', '-'],
            ['-', '-', '-', '2', '7', '1', '3', '-', '-']]

new_lst, tmp = [], []
for i in range(0, len(some_lst), 3):
    for j in range(len(some_lst)):
        tmp.extend(some_lst[j][i:i 3])
        if len(tmp) == 9:
            new_lst.append(tmp)
            tmp = []
print(*new_lst, sep="\n")

OutPut:

['-', '3', '4', '5', '2', '7', '-', '6', '-']
['-', '-', '5', '3', '9', '-', '-', '4', '-']
['1', '-', '-', '2', '-', '-', '-', '-', '-']
['-', '-', '6', '-', '1', '8', '-', '-', '9']
['-', '4', '-', '8', '-', '-', '-', '-', '2']
['6', '8', '4', '-', '-', '-', '2', '7', '1']
['-', '8', '-', '6', '-', '3', '4', '-', '2']
['-', '-', '-', '5', '-', '-', '9', '3', '6']
['-', '2', '-', '8', '1', '-', '3', '-', '-']
  • Related