Python Bore Automation, Chapter 4 Exercise

I am new and am making Al Sweigar book at the moment. In an exercise in chapter 4, he asks the following:

Let's say you have a list of lists, where each value in the internal lists is a single-character string, for example:

grid = [['.', '.', '.', '.', '.', '.'], ['.', 'O', 'O', '.', '.', '.'], ['O', 'O', 'O', 'O', '.', '.'], ['O', 'O', 'O', 'O', 'O', '.'], ['.', 'O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O', '.'], ['O', 'O', 'O', 'O', '.', '.'], ['.', 'O', 'O', '.', '.', '.'], ['.', '.', '.', '.', '.', '.']] 

You can think of the grid [x] [y] as a character in the x- and y-coordinates of a picture with text characters. The origin (0, 0) will be in the upper left corner, the coordinates of the x-coordinates go to the right, and the w y-coordinates increase. Copy the previous grid value and write the code that uses it to print the image.

 ..OO.OO.. .OOOOOOO. .OOOOOOO. ..OOOOO.. ...OOO... ....O.... 

So, I wrote the code, and it does what it asks for, but I think it is very poorly written, and I wanted to ask you how I can improve it. My code

 grid = [['.', '.', '.', '.', '.', '.'], ['.', 'O', 'O', '.', '.', '.'], ['O', 'O', 'O', 'O', '.', '.'], ['O', 'O', 'O', 'O', 'O', '.'], ['.', 'O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O', '.'], ['O', 'O', 'O', 'O', '.', '.'], ['.', 'O', 'O', '.', '.', '.'], ['.', '.', '.', '.', '.', '.']] newString = '' for i in range(len(grid)): newString += str(grid[i][0]) newString1 = '\n' for i in range(len(grid)): newString1 += str(grid[i][1]) newString2 = '\n' for i in range(len(grid)): newString2 += str(grid[i][2]) newString3 = '\n' for i in range(len(grid)): newString3 += str(grid[i][3]) newString4 = '\n' for i in range(len(grid)): newString4 += str(grid[i][4]) newString5 = '\n' for i in range(len(grid)): newString5 += str(grid[i][5]) print(newString+newString1+newString2+newString3+newString4+newString5) 

Program Output:

 ..OO.OO.. .OOOOOOO. .OOOOOOO. ..OOOOO.. ...OOO... ....O.... 
+5
source share
22 answers
 >>> print('\n'.join(map(''.join, zip(*grid)))) ..OO.OO.. .OOOOOOO. .OOOOOOO. ..OOOOO.. ...OOO... ....O.... 

zip(*grid) efficiently transfers the matrix (flips it along the main diagonal), then each line is combined into one line, then the lines are connected with newline characters, so all this can be printed immediately.

+9
source

I'm new too. Using only what the book examined, and keeping in mind the loop in the loop tip, this is my answer:

 for j in range(len(grid[0])): for i in range(len(grid)): print(grid[i][j],end='') print('') 
+12
source

I have another simple solution, very similar to other solutions using only for loops. But one thing that I think has changed is that I used two additional operators: one inside the inner loop, one outside. I thought it would be useful after figuring out what we were being asked.

 print(grid[0][0], grid[1][0], grid[2][0], grid[3][0], grid[4][0], grid[5][0], grid[6][0], grid[7][0], grid[8][0]) 

Print statement output:

., O O. O O ..

As you can see, this is the first row of the heart grid. We need to count from 0 to len (grid [0]) , that is, the number of elements in the first list, you can simply enter 6. So, all I need is two operators that count each other. The empty print statement is for line breaks. If we do not use it, it prints either all the characters on one line, or each character on each line.

Decision:

 def printer(grid): for m in range(len(grid[0])): print() for n in range(len(grid)): print (grid[n][m],end="") n+=1 m+=1 

Conclusion:

 ..OO.OO.. .OOOOOOO. .OOOOOOO. ..OOOOO.. ...OOO... ....O.... 
+2
source
 grid = [['.', '.', '.', '.', '.', '.'], ['.', 'O', 'O', '.', '.', '.'], ['O', 'O', 'O', 'O', '.', '.'], ['O', 'O', 'O', 'O', 'O', '.'], ['.', 'O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O', '.'], ['O', 'O', 'O', 'O', '.', '.'], ['.', 'O', 'O', '.', '.', '.'], ['.', '.', '.', '.', '.', '.']] def gridOutput(grid): for s in range(len(grid[0])): print() for i in range(len(grid)): print(grid[i][s],end='') gridOutput(grid) 

Only the information that I learned in previous chapters is used. I appreciate reading more advanced techniques for the same problem.

+2
source

The author says to use a loop in a loop. Here is my answer:

 def reformat(myList): for i in range(0,len(myList[0])): myStr = '' for j in range(0,(len(myList))): myStr += myList[j][i] print(myStr) 
+1
source

One way to solve the problem (given the prompt in the book) is to identify the rows and columns. This list contains 9 rows (list items) and 6 columns (length of each item).

Clearly the problem is getting -

a) Print all column values ​​for row 1 b) Print all column values ​​for row 2 ... etc.

This approach can be “standardized” if we measure the number of rows and columns that need to be printed in advance.

The following program illustrates one way to do this -

 # This program illustrates the character picture grid grid = [['.', '.', '.', '.', '.', '.'], ['.', 'O', 'O', '.', '.', '.'], ['O', 'O', 'O', 'O', '.', '.'], ['O', 'O', 'O', 'O', 'O', '.'], ['.', 'O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O', '.'], ['O', 'O', 'O', 'O', '.', '.'], ['.', 'O', 'O', '.', '.', '.'], ['.', '.', '.', '.', '.', '.']] rows = len(grid) # Number of elements in the list cols = len(grid[0]) # Number of elements in every single element in the list for j in range(cols): for i in range(rows): print(grid[i][j], end='') print() 
+1
source

Good decisions! I am not so comfortable using map (). As for the exercise, I set myself the task of flipping the grid 90 degrees.

First, it creates a grid with the same number of lists as the length of a single list in the original grid (to get the x axis). Then it adds the first (or nth) elements of each list of the original mesh to the first (or nth) list of the new mesh and displays each row of the new mesh.

This can be done in fewer lines of code, but here it is:

 def character_picture_grid(x): """The character_picture _grid print a grid list with list(9 x 6) and rotates it 90-degrees.""" # Flip the grid. newGrid = [[] for i in range(len(x[0]))] for i in range(len(newGrid)): for a in range(len(x)): newGrid[i].append(x[a][i]) # Print the grid for i in newGrid: print(i) 

EDIT: Grammar.

0
source

It is important to remember that the idea of ​​the exercise is to resolve it with what you learned before this chapter, so there is one way to use the loop in a loop:

 def grilla(x): for y in range(6): print() for i in range(len(x)): print(x[i][y], end='') grilla(grid) 
0
source

Hi guys, I'm new to python, here is my answer.

 for i in range(len(grid[1])): print(' ') for z in range(len(grid)): print(grid[z][i], end='') 
0
source

I also took a hint from the author about using a loop in a loop. So I actually used a combination of time and cycle. Here is my code:

 #First I copied the grid as the author instructs grid = [['.','.','.','.','.','.'], ['.','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','.','.','.'], ['.','.','.','.','.','.']] # I set two global variables x and y. I will use x to iterate through # the while loop and y to increment the index in the inner list x = 0 y = 0 # set the condition for the while loop to limit it to the length of the list # and also to limit it to the length of the inner list which doesn't exceed index 5 while x < len(grid) and y <= 5: for item in range(len(grid)): print(grid[item][y], end='') y += 1 # increment y after iteration of a for loop print('') # print blank line x += 1 # increment x after iteration of for loop go through for loop again 

Output

 ..00.00.. .0000000. .0000000. ..00000.. ...000... ....0.... 
0
source
 def heart(): #Set your range to the length of the first list in grid. for y in range(len(grid[0])): #This print('') will break every printed line once the second for loop completes below. print('') #Set your second for loop to the length of the number of lists within grid. for x in range(len(grid)): #Add an end keyword arg to allow for consecutive str prints without new lines. print(grid[x][y],end='') heart() 

 def heart(): for y in range(len(grid[0])): print('') for x in range(len(grid)): print(grid[x][y],end='') heart() 

^ code without comments.

Think of the pattern that Al asks for. [0] [0], [0] [1], [0] [2] ... [8] [0], [8] [1], [8] [2]. Thus, you basically want the first one to indicate in which list the data is retrieved, and then inside to select the index to print. The arg keyword is also crucial.

0
source

You can do it even like this:

 for i in range(len(grid[])): for j in range(len(grid)): print(grid[j][i],end = " ") print() 
0
source

My attempt, as well as unification and time. Not as pretty as the previous answers, but still enough for me.

 grid = [['.', '.', '.', '.', '.', '.'], ['.', 'O', 'O', '.', '.', '.'], ['O', 'O', 'O', 'O', '.', '.'], ['O', 'O', 'O', 'O', 'O', '.'], ['.', 'O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O', '.'], ['O', 'O', 'O', 'O', '.', '.'], ['.', 'O', 'O', '.', '.', '.'], ['.', '.', '.', '.', '.', '.']] x=0 #counter y=0 #counter for y in range(len(grid[x])): #for each y in sublists new_str="" #create temporary new string to house the sublists while x <= len(grid)-1: #for each x in list new_str += grid[x][y] x+=1 #add every x in a string while keeping y steady print(new_str) #print string of x while y steady x=0 #set x to 0 in order to start again on next iteration of y 
0
source
 for j in range(len(grid[:6])): # print(grid[0][j]) for i in range(len(grid)): print(grid[i][j], end='') print() 

The recorded part printed a single dot on the first line. The digit O itself was correct, some other points were absent in the first column.

0
source

I am also a beginner, and I used only what the book covered. This is my code.

 for i in range(len(grid[0])): for j in range(len(grid)): if j == (len(grid)-1): print(grid[j][i]) else: print(grid[j][i], end='') 
0
source

Here is my answer:

 grid = [['.', '.', '.', '.', '.', '.'], ['.', 'O', 'O', '.', '.', '.'], ['O', 'O', 'O', 'O', '.', '.'], ['O', 'O', 'O', 'O', 'O', '.'], ['.', 'O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O', '.'], ['O', 'O', 'O', 'O', '.', '.'], ['.', 'O', 'O', '.', '.', '.'], ['.', '.', '.', '.', '.', '.']] for i in range(len(grid[0])): col=[col[i] for col in grid] print(*col, sep='') 
0
source

Also new to python, my answer is below and it works.

  for i in range (0,6,1):
     for j in range (0,8,1):     
         print (grid [j] [i], end = '')
     print ('')
0
source
 grid = [['.', '.', '.', '.', '.', '.'], ['.', 'O', 'O', '.', '.', '.'], ['O', 'O', 'O', 'O', '.', '.'], ['O', 'O', 'O', 'O', 'O', '.'], ['.', 'O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O', '.'], ['O', 'O', 'O', 'O', '.', '.'], ['.', 'O', 'O', '.', '.', '.'], ['.', '.', '.', '.', '.', '.']] def love_grid(name): xyz = int(len(list(name)) - 1) for a in range(0, len(list(name))): print(name[a][0], end="") if a == xyz: print() break for a in range(0, len(list(name))): print(name[a][1], end="") if a == xyz: print() break for a in range(0, len(list(name))): print(name[a][2], end="") if a == xyz: print() break for a in range(0, len(list(name))): print(name[a][3], end="") if a == xyz: print() break for a in range(0, len(list(name))): print(name[a][4], end="") if a == xyz: print() break for a in range(0, len(list(name))): print(name[a][5], end="") if a == xyz: print() break love_grid(grid) 
0
source
 def grid(l): for i in range(len(l)): print('') for j in range(len(l[i])): s=l[j][i] print(s,end='') 

This also applies to commas.

0
source

A simple way:

 for i in range(len(grid[1])): for row in grid: print (row[i], end='') print() 
0
source
 for x in range (0,6): for y in range (0,9): print (grid[y][x], end='') print ('') 
-1
source
 b=len(grid) for y in range(b-3): print('\n') for x in range(b): print(grid[x][y], end=' ') 
-2
source

All Articles