Python - Sorting huge array into multiples

Refresh

February 2019

Views

55 time

1

I'm working with a map of which is split up into 750 x 750 numbered tiles. These are stored in a JSON file. My aim is to pull the data and split each tile into separate arrays / chunks.

I already have it working, but I'm a python beginner and the code is really slow (it is handling 500,000+ numbers). Wondered if there was any faster method available for doing the same thing.

First I pull the json data into an array

with open("data.json") as data_file:
    data = json.load(data_file)
    arr = data["mapLayer"][0]["tiles"]

Then I run through the array and sort them into separate chunk arrays based on their position:

x=0
y=0 

for tile in arr:
    chunkPos = str(x/16) + "x" + str(y/16)
    chunks[chunkPos].append(tile)
    x += 1

    // reset x pos when it reaches map edge
    if x == 750
        x=0
        y=y+1

1 answers

0

you can store your tiles in numpy array and than just reshape your array using 4 dimensions (i assume that 256 is your chunk size)

import numpy as np

 with open("data.json") as data_file:
    data = json.load(data_file)
    arr = np.array(data["mapLayer"][0]["tiles"])


arr = np.reshape(arr, (750, 1,750, 256))