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

How do I print a dictionary in python? (Prints it one by one on a separate line)

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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),
    }

这是我的代码,我不知道怎么做这个becuz我新谢谢你的帮助:)

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


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

输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
    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),

您导致使用内置的print()函数

1
print(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)}

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

1
2
3
4
import pprint

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

给你的

1
2
3
4
5
6
7
8
9
10
11
12
13
{   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)}