How can I use x, y coordinate matrices to choose where a character appears in a string with python?
<p>I am attempting to make a simple maze game to test a NNS with genetic algorithms.
the maze for each test would use a matrix to hold the x, y points of things like barriers, the start, the end, and the player's current position. The main thing that I need help with is placing the right character in the right location in a string so when the strings of row 1-25 are read (probably with a for loop) it will read out a layout of the map. As an example, the barrier points 1,1 3,4 and 1,5 would look like this if an "o" is a space:
first string<code>|XoooX|</code>,
second string<code>|ooooo|</code>,
third string<code>|ooooo|</code>,
fourth string<code>|ooXoo|</code>.
Any ideas? Thanks in advance!</p>

<p>You're probably looking for something along the lines of this:</p>

<pre><code>width = 6
height = 6
coords = [(1,1),(3,4),(1,5)]
print('\n'.join(['|' + ''.join(['x' if (x,y) in coords else 'o' for x in range(width)]) + '|' for y in range(height)]))
</code></pre>

<p>Using list comprehension, we can easily construct each row one at a time:</p>

<pre><code>''.join(['x' if (x,y) in coords else 'o' for x in range(width)])
</code></pre>

<p>Basically, print an <code>x</code> if the coordinate contains something, otherwise use <code>o</code>.</p>

<p>Add the side bars in...</p>

<pre><code>'|' + ''.join([...]) + '|'
</code></pre>

<p><code>''.join()</code> is a <a href="https://docs.python.org/3/library/stdtypes.html#str.join" rel="nofollow">very useful function</a> for this.</p>

<p>Then all we have to do is repeat this for each row, making sure to insert a line break between each row.</p>

<pre><code>'\n'.join([...])
</code></pre>

python arrays string matrix coordinate