如何编号打印字典

时间:2018-04-16 04:47:50

标签: python-3.x

ice_cream = {'I Vanilla': [1.25, 10], 'I Chocolate': [1.25, 8], 'I Cookies & Cream': [1.51, 15]}  #{ice cream type: price, quantity}

def show_inventory():
    print('\nINVENTORY\n')
    print('Ice Cream')
    for key, value in ice_cream.items():
        print("{:15} \t {:5} \t {}".format(key, *value))

这是我的结果

I Vanilla             1.25   10
I Chocolate           1.25   8
I Cookies & Cream     1.51   15

但是,我希望它看起来像这样......

1 ) I Vanilla             1.25   10
2 ) I Chocolate           1.25   8
3 ) I Cookies & Cream     1.51   15
4 ) future additions
5 ) future additions

1 个答案:

答案 0 :(得分:0)

DyZ的意思是这个

ice_cream = {'I Vanilla': [1.25, 10], 'I Chocolate': [1.25, 8], 'I Cookies & Cream': [1.51, 15]}  #{ice cream type: price, quantity}

def show_inventory():
    print('\nINVENTORY\n')
    print('Ice Cream')
    for n, (key, value) in enumerate(ice_cream.items(), 1):
        print("{}) {:15} \t {:5} \t {}".format(n, key, *value))