Detailed discussion based on Python3 comma code and character graph grid of


The comma code

Suppose you have the following list:

spam=['apples','bananas','tofu',' cats']

Write a function that takes a list value as an argument and returns a string. The string contains all table entries, separated by commas and Spaces, and inserts and before the last one. For example, the preceding spam list passed to a function, returns’ apples, bananas tofu, and cats ’. But your function should be able to pass to any list it has.

The code is as follows:

import copy

def conFun(nameList):
  n=len(nameList)
  newList=copy.copy(nameList)
  newList.insert(n-1,'and')
  # print(newList)
  a=str(newList.pop())
  b=str(newList.pop())
  c=''
  c=b+' '+a
  newOne=''
  newOne=newList[0]
  i=1
  for j in newList:
    newOne=newOne+','+newList[i]
    i=i+1
    if i==len(newList):
      break
  print(newOne+','+c)

Verification code:

================== RESTART: /Users/valen/Documents/test.py ==================
>>> spam=['apple','bananas','tofu','cats']
>>> conFun(spam)
apple,bananas,tofu,and cats
>>>

Character graph grid

Suppose there is a list of lists, and each value of the inner list is a 1-character string, like this:

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', '.', '.','.'],
     ['.', '.', '.', '.', '.','.']]

You can think of grid[x][y] as the character of 1 “graph” at the x,y coordinates. The graph consists of text characters. At the origin (0,0) in the upper left corner, the x coordinates increase to the right, and the y coordinates increase to the downward.

Copy the previous grid value and write code to print the image using it.

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

Tip: You need to use nested loops, print out grid[0][0], then grid[1][0], then grid[2][1], and so on, until grid[8][0]. This completes line 1, so the next step is to print a newline. The program will then print grid[0][1], then grid[1][1], then grid[2][1], and so on. The program will print grid[8][5] at the end.

Also, if you don’t want to automatically print line breaks after every call to print(), remember to pass the end keyword argument to print().

import copy

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', '.', '.','.'],
     ['.', '.', '.', '.', '.','.']]
c=[]
c=copy.deepcopy(grid)
#print(c)
gridLen=len(grid)
cyctime=len(grid[0])
#print(cyctime)
i=0
j=0
for j in range(cyctime):
  if j < cyctime :
    for i in range(gridLen):
      if i < gridLen :
        print(c[i][j],end=' ')
        i=i+1
  print('\n')
  j=j+1

The output is as follows:

================== RESTART: /Users/valen/Documents/test.py ==================
. . 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 . . . .

>>>