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.
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