如何在python中打印字典? (在另一条线上逐一打印)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在python中打印字典? (在另一条线上逐一打印)相关的知识,希望对你有一定的参考价值。
我想在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),
}
这是我的代码,我不知道怎么做,因为我是新的感谢帮助:)
编辑:我如何制作它,以便逐个打印每个列表。
答案
你使用内置的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)}
另一答案
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),
以上是关于如何在python中打印字典? (在另一条线上逐一打印)的主要内容,如果未能解决你的问题,请参考以下文章
Educational Codeforces Round 41