如何在python中打印字典? (在另一条线上逐一打印)

问题描述 投票:-3回答:2

我想在python中打印一本字典。

menu = {
        1: ("Single Red Roses", 29),
        2: ("Burst of Spring Posy", 59),
        3: ("Super Grade Red Roses", 69),
        4: ("Lemon Gelato Roses", 79),
        5: ("Market Fresh Pin Lily", 79),
        6: ("Summer Daisy Gerbera", 79),
        7: ("Bag of Garden Roses", 89),
        8: ("Blue Lime Spring Rosy", 89),
        9: ("Hot Pink Roses", 89),
        10: ("Class White Roses", 99),
        11: ("Fresh Lime", 109),
        12: ("Boxed Red Roses", 129),
        13: ("Tropical Rain-forest Bouquet", 149),
    }

这是我的代码,我不知道怎么做,因为我是新的感谢帮助:)

编辑:我如何制作它,以便逐个打印每个列表。

python pycharm
2个回答
2
投票

你使用内置的print()功能

print(menu)

{1:('单红玫瑰',29),2 :('春天的爆裂',59),3:('超级红玫瑰',69),4:('柠檬冰淇淋',79) ,5:('Market Fresh Pin Lily',79),6:('Summer Daisy Gerbera',79),7:('袋子花园玫瑰',89),8:('Blue Lime Spring Rosy',89 ),9:('Hot Pink Roses',89),10:('Class White Roses',99),11:('Fresh Lime',109),12:('盒装红玫瑰',129),13 :('热带雨林花束',149)}

对于控制台的格式化输出,您可以使用pprint模块

import pprint

pp = pprint.PrettyPrinter(indent=4)
pp.pprint(menu)

给你的

{   1: ('Single Red Roses', 29),
    2: ('Burst of Spring Posy', 59),
    3: ('Super Grade Red Roses', 69),
    4: ('Lemon Gelato Roses', 79),
    5: ('Market Fresh Pin Lily', 79),
    6: ('Summer Daisy Gerbera', 79),
    7: ('Bag of Garden Roses', 89),
    8: ('Blue Lime Spring Rosy', 89),
    9: ('Hot Pink Roses', 89),
    10: ('Class White Roses', 99),
    11: ('Fresh Lime', 109),
    12: ('Boxed Red Roses', 129),
    13: ('Tropical Rain-forest Bouquet', 149)}

2
投票
for key,val in menu.items():
    print(key + ": " + val)

输出:

    1: ("Single Red Roses", 29),
    2: ("Burst of Spring Posy", 59),
    3: ("Super Grade Red Roses", 69),
    4: ("Lemon Gelato Roses", 79),
    5: ("Market Fresh Pin Lily", 79),
    6: ("Summer Daisy Gerbera", 79),
    7: ("Bag of Garden Roses", 89),
    8: ("Blue Lime Spring Rosy", 89),
    9: ("Hot Pink Roses", 89),
    10: ("Class White Roses", 99),
    11: ("Fresh Lime", 109),
    12: ("Boxed Red Roses", 129),
    13: ("Tropical Rain-forest Bouquet", 149),
© www.soinside.com 2019 - 2024. All rights reserved.