Advertisement
Guest User

Untitled

a guest
Feb 28th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. def record_disp(info):
  2. a=[list(x) for x in info]
  3. table=[]
  4. lens=[]
  5. for x in range(len(a[0])):
  6. maxlen=0
  7. for y in range(len(a)):
  8. maxlen=len(str(a[y][x])) if len(str(a[y][x]))>maxlen else maxlen
  9. lens.append(maxlen)
  10. for x in range(len(a)):
  11. for y in range(len(a[x])):
  12. print(str(a[x][y])+(" "*(lens[y]-len(str(a[x][y])))),end=" |")
  13. print()
  14.  
  15. def record_disp(info): #define a function that takes the input data, info
  16. a=[list(x) for x in info] #turn the input list into a list of lists
  17. lens=[] # make an empty list to store max lengths
  18.  
  19. #loop with x values from 0 to length of first list
  20. for x in range(len(a[0])):
  21. # get the longest item
  22. maxlen=0
  23. for y in range(len(a)):
  24. maxlen=max(len(str(a[y][x])), maxlen)
  25. lens.append(maxlen)
  26. #for every item print it out table, formatting with spaces
  27. for x in range(len(a)): #for each row
  28. for y in range(len(a[x])): # for each column index in the row
  29. # the previously calculated column width (lens[y])
  30. # is used to work out the number of spaces needed
  31. spacing_needed = lens[y]-len(str(a[x][y]))
  32. # print one cell, spacing it out and delimiting with |
  33. print(str(a[x][y])+" "*spacing_needed,end=" |")
  34. print() # adds a newline at the end of the row
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement