以下は私のコードです:
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")
私はこのようなものが欲しい:
Inventory:
count item name
----------------
45 gold coin
12 arrow
6 torch
2 dagger
1 rope
1 ruby
----------------
Total number of items: 67
順序パラメータは文字列である必要があります。これは次のように機能します: 空 (デフォルト) は、テーブルが順序付け
"count,desc"
されていないことを意味します。テーブルは (インベントリ内のアイテムの) カウントで降順および"cound,asc"
昇順で並べられます。
すべての文字列に収まる列の幅を知るために、内部リストのそれぞれで最長の文字列を見つける関数を書きましたが、この時点で行き詰まっています。どうすればよいですか?