954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Array and formating text - matrix

Hi,
How can I print this figure if I have an array like this?
It should be a for-loop but don´t know how.

labyrinth = \
[[0,0,0,0,0,0,1,0],
[0,1,0,1,1,1,1,0],
[0,1,1,1,0,1,0,0],
[0,1,0,0,0,0,0,0],
[0,1,1,0,1,1,1,0],
[0,0,1,1,1,0,0,0],
[0,1,1,0,1,1,1,0],
[0,1,0,0,0,0,0,0]]


figure:
############. ##
## ##. . . . ##
##. . . ## ####
##. ############
##. . ## ##
####. ######
##. . ## ##
##. ############

sallubhai
Newbie Poster
7 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 
from __future__ import print_function # 

labyrinth = \
[[0,0,0,0,0,0,1,0],
[0,1,0,1,1,1,1,0],
[0,1,1,1,0,1,0,0],
[0,1,0,0,0,0,0,0],
[0,1,1,0,1,1,1,0],
[0,0,1,1,1,0,0,0],
[0,1,1,0,1,1,1,0],
[0,1,0,0,0,0,0,0]]

[[print("##" if char==0 else ".",end="" if i<len(line)-1 else "\n") for i,char in enumerate(line)]  for line in labyrinth]
slate
Posting Whiz in Training
252 posts since Jun 2008
Reputation Points: 72
Solved Threads: 66
 

Make if statement in midle of for using values as True=1 or False=0

pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

Wooo Slate and tonyjv are really complex

Infact the pseudo of slate is flawless but will a newbie understand the algos.?

I just wrote this as a step by step to the solution. I real life, i advice you to use Slate's example.

labyrinth = \
[[0,0,0,0,0,0,1,0],
[0,1,0,1,1,1,1,0],
[0,1,1,1,0,1,0,0],
[0,1,0,0,0,0,0,0],
[0,1,1,0,1,1,1,0],
[0,0,1,1,1,0,0,0],
[0,1,1,0,1,1,1,0],
[0,1,0,0,0,0,0,0]]

de=0
for x in labyrinth:
	for y in x:
		de=de+1
		if y == 0:
			print '##',
		else:
			print ".",
		if de == 8:
			print '\n'
			de=0


for fun ;)

richieking
Master Poster
764 posts since Jun 2009
Reputation Points: 61
Solved Threads: 152
 

We are not supposed to give ready answers. After this your try I must write cleaner version, though. Use 4 space tabs next time.
I prefer not to use line continuation character, but utilize automatic continuation inside parenthesis and brackets:

from __future__ import print_function

labyrinth = [
             [0,0,0,0,0,0,1,0],
             [0,1,0,1,1,1,1,0],
             [0,1,1,1,0,1,0,0],
             [0,1,0,0,0,0,0,0],
             [0,1,1,0,1,1,1,0],
             [0,0,1,1,1,0,0,0],
             [0,1,1,0,1,1,1,0],
             [0,1,0,0,0,0,0,0]]

for line in labyrinth:
    for isopen in line:
        print('  ' if isopen else "##", end='')
    print()
print('-'*40)

# in real life
print('\n'.join(''.join('  ' if isopen else "##" for isopen in line)
                        for line in labyrinth))
pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

wow thanks, now it is Ok!

sallubhai
Newbie Poster
7 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

but tonyjv there is a problem now, if you look at the figure there are some dots, how can I print them just like the figure?

sallubhai
Newbie Poster
7 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

The codes are not there, you maybe must apply one labyrinth solver algorith to find the path, like mine, which solved the labyrinth and when I set the marking of path properly, and gave open places as S and E gave results like this:

The solved labyrinth
############ .##
##  ## .......##
## .....##  ####
## .############
## .. ##      ##
####.     ######
## .. ##      ##
## .############


7.820 ms
Enter to finish
pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: