Home > Net >  Python Np Array
Python Np Array

Time:02-20

I have following python code, where i am struggling to get the right results as described in question.

import numpy as np

number_of_ants = 500

ameisen = np.array([
    [number_of_ants, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
])

labyrinth = np.array([
    [False, True, False, False, False, False, True, False, False, False], 
    [False, True, False, False, True, False, True, False, False, False], 
    [False, True, True, False, True, False, True, False, False, False], 
    [False, True, False, False, True, False, True, True, True, True], 
    [False, False, False, True, True, False, False, False, False, True], 
    [False, True, False, True, True, False, True, True, False, True], 
    [False, True, False, False, True, False, True, False, False, True], 
    [False, True, True, False, True, False, True, False, True, True], 
    [False, True, False, False, True, False, True, False, False, False], 
    [False, True, False, False, True, False, True, True, True, True], 
])


def print_labyrinth(step):
    print(f"[{step}]")
    
    new_array = ameisen   labyrinth
    
    new_array = new_array.tolist()
    
    print(new_array)

       
    return

ef move_all_ants_one_step():
    # START TODO b)
    pass
    # END TODO


def simulate(number_of_ants):
    # START TODO c)
    pass
    # END TODO


def analysis():
    # START TODO d)
    print_labyrinth(0)
    # END TODO

analysis()

When I execute it, I got following result:

[0]
[[500, 1, 0, 0, 0, 0, 1, 0, 0, 0], [0, 1, 0, 0, 1, 0, 1, 0, 0, 0], [0, 1, 1, 0, 1, 0, 1, 0, 0, 0], [0, 1, 0, 0, 1, 0, 1, 1, 1, 1], [0, 0, 0, 1, 1, 0, 0, 0, 0, 1], [0, 1, 0, 1, 1, 0, 1, 1, 0, 1], [0, 1, 0, 0, 1, 0, 1, 0, 0, 1], [0, 1, 1, 0, 1, 0, 1, 0, 1, 1], [0, 1, 0, 0, 1, 0, 1, 0, 0, 0], [0, 1, 0, 0, 1, 0, 1, 1, 1, 1]]

Where I am looking for way to get the following result:


[0]
[500][---][000][000][000][000][---][000][000][000]
[000][---][000][000][---][000][---][000][000][000]
[000][---][---][000][---][000][---][000][000][000]
[000][---][000][000][---][000][---][---][---][---]
[000][000][000][---][---][000][000][000][000][---]
[000][---][000][---][---][000][---][---][000][---]
[000][---][000][000][---][000][---][000][000][---]
[000][---][---][000][---][000][---][000][---][---]
[000][---][000][000][---][000][---][000][000][000]
[000][---][000][000][---][000][---][---][---][---]

I am new to Python, I have tried many ways but did not get any success. Probably, [000] & [---] need to be defined as well with line breaks?

Any Help?

CodePudding user response:

There are plenty of ways to do this, one way is as follows below

def print_labyrinth(step):
    print(f"[{step}]")

    for row_ameisen, row_labyrinth in zip(ameisen, labyrinth):
        for ameise, maze_bool in zip(row_ameisen, row_labyrinth):
            if not maze_bool:
                print(f"[{ameise:03d}]", end="")
            else:
                print("[---]", end="")
        print("")

Maybe I would recomend using numpy arrays, since I don't think you are going to use numpy build-in functions, but rather only manipulate elements using Python logic.

CodePudding user response:

By default, square brackets surround an entire array, not each element in an array, so Python won't output [500][---][000]...

You can try looping through your array of arrays, and print it in the format you want:

for row in ameisen:
   for cell in row:
       print("[ = ]" % (cell), end = " ")
  • Related