Following is my code:
inv = {'rope': 1, 'torch': 6, 'gold coin': 42, 'dagger': 1}
x=max(inv, key=lambda x: len(x.split()))
y=len(x)
#print(y)
n = "-"
q = n * (y+5)
#print(q)
#print("")
def print_table(inventory, order=None):
if order=="count,asc":
for key, value in sorted(inventory.iteritems(), key=lambda (k, v): (v, k)):
print "%s %s" % (value, key)
print_table(inv,"count,asc")
I want to have something like this:
Inventory:
count item name
----------------
45 gold coin
12 arrow
6 torch
2 dagger
1 rope
1 ruby
----------------
Total number of items: 67
Order parameter must be a string, which works as following:
empty (by default) means the table is unordered
"count,desc"
table is ordered by count (of items in the inventory) in descending order and "cound,asc"
in ascending order
I wrote function that's finding longest string in each of the inner lists to know how wide should be column to fit all strings, but now I'm stuck at this point, what should I do now?