1

I'm trying to get my data to display in a nice format using a table, all other google searches give me really confusing over complicated solutions and i was wondering if there was an easier way of doing it. I'm using using this code:

print("no. of cities | Depth-first | Breadth-first | Greedy Search")
for num in n:
    ...
    print("%d | %d | %d | %d | %d" %(n,depth_count,breadth_count,greedy_count, total))

Which gives me the result:

no. of cities | Depth-first | Breadth-first | Greedy Search | Total
5 | 24 | 24 | 10 | 58
6 | 120 | 120 | 15 | 255
...

but I would like:

no. of cities | Depth-first | Breadth-first | Greedy Search | Total
5             | 24          | 24            | 10            | 58
6             | 120         | 120           | 15            | 255
...

Any help appreciated.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Ben Graham
  • 65
  • 5

2 Answers2

0

Have a look at Pandas. With the data-frames you can visualize data that way.

nocab
  • 156
  • 1
  • 11
0

There are some good answers on this post, but if you want something simple you could use fixed-width formatting. For example:

n,depth_count,breadth_count,greedy_count, total = 5, 24, 24, 10, 58
header = ('no. of cities', 'Depth-first', 'Breadth-first', 'Greedy Search', 'Total')
print("%15s|%15s|%15s|%15s|%15s" % header)
print("%15d|%15d|%15d|%15d|%15d" %(n,depth_count,breadth_count,greedy_count, total))

#  no. of cities|    Depth-first|  Breadth-first|  Greedy Search|          Total
#              5|             24|             24|             10|             58

Here the %15d means print an integer right-justified using a length 15 string.

If you instead wanted to print left-justified, you can use %-15:

print("%-15s|%-15s|%-15s|%-15s|%-15s" % header)
print("%-15d|%-15d|%-15d|%-15d|%-15d" %(n,depth_count,breadth_count,greedy_count, total))
#no. of cities  |Depth-first    |Breadth-first  |Greedy Search  |Total          
#5              |24             |24             |10             |58  
pault
  • 41,343
  • 15
  • 107
  • 149