简体   繁体   中英

Connecting 2D lists into a larger 2D list in Python

If I have a function which randomly returns 2D lists of the same size, how would I "tile" them together?

For example, if I generate 4 2D lists which are 3 by 3 in size, how would I combine them in a 2 by 2 arrangement into a 6 by 6 2D list?

    [[0,0,0],   [[1,1,1],   [[2,2,2],   [[3,3,3],
     [0,0,0], +  [1,1,1], +  [2,2,2], +  [3,3,3],
     [0,0,0]]    [1,1,1]]    [2,2,2]]    [3,3,3]]

Arranged h =2 by w =2 makes:

    [[0,0,0,1,1,1],
     [0,0,0,1,1,1],
     [0,0,0,1,1,1],
     [2,2,2,3,3,3],
     [2,2,2,3,3,3],
     [2,2,2,3,3,3]]

In my case the individual lists are generated randomly and returned by a function which takes width and height as arguments.

I need to specify some dimensions ( h and w ) and arrange (h*w) random sub-grids into an h by w super-grid. The order/specific arrangement of the sub-grids doesn't matter, one after the other or any other arrangement is fine.

How would I go about doing this if I want a function that takes as arguments width and height of the super-grid, and width and height of the sub-grids?

It's pretty much just a matter of grinding through the appropriate subsets of the list of subgrids. (There might be some elegant way to do this in a single nested comprehension, but I'm not seeing it, lol.)

from typing import List, TypeVar

_GridItem = TypeVar('_GridItem')


def tile(
    h: int,
    w: int,
    subgrids: List[List[List[_GridItem]]]
) -> List[List[_GridItem]]:
    tiled_grid: List[List[_GridItem]] = []
    for _ in range(h):
        tile_row = subgrids[:w]
        subgrids = subgrids[w:]
        for i in range(len(tile_row[0])):
            tiled_grid.append([x for subgrid in tile_row for x in subgrid[i]])
    return tiled_grid

This is sufficiently flexible that it'll work with any h , w , and conforming subgrids (ie len(subgrids) == w*h , and the dimensions of all subgrids are the same -- it wouldn't be too hard to add a couple of checks to the function to enforce this). Here's an example of using this function to tile 8 4x2 subgrids in a 2x4 layout:

print("\n".join(map(str, tile(2, 4, [
    [[n for _ in range(2)] for _ in range(4)] for n in range(8)
]))))

yields:

[0, 0, 1, 1, 2, 2, 3, 3]
[0, 0, 1, 1, 2, 2, 3, 3]
[0, 0, 1, 1, 2, 2, 3, 3]
[0, 0, 1, 1, 2, 2, 3, 3]
[4, 4, 5, 5, 6, 6, 7, 7]
[4, 4, 5, 5, 6, 6, 7, 7]
[4, 4, 5, 5, 6, 6, 7, 7]
[4, 4, 5, 5, 6, 6, 7, 7]

If I have a function which randomly returns 2D lists of the same size, how would I "tile" them together?

For example, if I generate 4 2D lists which are 3 by 3 in size, how would I combine them in a 2 by 2 arrangement into a 6 by 6 2D list?

    [[0,0,0],   [[1,1,1],   [[2,2,2],   [[3,3,3],
     [0,0,0], +  [1,1,1], +  [2,2,2], +  [3,3,3],
     [0,0,0]]    [1,1,1]]    [2,2,2]]    [3,3,3]]

Arranged h =2 by w =2 makes:

    [[0,0,0,1,1,1],
     [0,0,0,1,1,1],
     [0,0,0,1,1,1],
     [2,2,2,3,3,3],
     [2,2,2,3,3,3],
     [2,2,2,3,3,3]]

In my case the individual lists are generated randomly and returned by a function which takes width and height as arguments.

I need to specify some dimensions ( h and w ) and arrange (h*w) random sub-grids into an h by w super-grid. The order/specific arrangement of the sub-grids doesn't matter, one after the other or any other arrangement is fine.

How would I go about doing this if I want a function that takes as arguments width and height of the super-grid, and width and height of the sub-grids?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM