有没有一种方法可以在每行中打印一个键和字典的值?

时间:2019-04-08 00:42:26

标签: python python-3.x

我想在字典中的每一行中打印我的键和值。

我尝试使用/ n或按Enter键,但这没用

pizzaTypes = {
    "Hawaiian":"$8.50", "Pepperoni":"$8.50", "Simply Cheese":"$8.50", "Ham & Cheese":"$8.50", "Beef & Onion":"$8.50", "Cheesy Garlic":"$8.50", "BBQ Pork & Onion":"$8.50", "Mr. Wedge":"$13.50", "Apricot Chicken":"$13.50", "Cranberry & Chicken":"$13.50", "BBQ Meatlovers":"$13.50", "Godfather":"$13.50"
    }

这是我的字典。谢谢

2 个答案:

答案 0 :(得分:4)

是的,实际上有。

0
5

这意味着对于每个键和每个值,它将分别打印,因此您的输出将是:

for x,y in pizzaTypes.items():
    print(x,y)

答案 1 :(得分:2)

您还可以漂亮地打印字典,在单独的行中打印dict键值对:

import pprint
pizzaTypes = {
    "Hawaiian":"$8.50", "Pepperoni":"$8.50", "Simply Cheese":"$8.50", "Ham & Cheese":"$8.50", "Beef & Onion":"$8.50", "Cheesy Garlic":"$8.50", "BBQ Pork & Onion":"$8.50", "Mr. Wedge":"$13.50", "Apricot Chicken":"$13.50", "Cranberry & Chicken":"$13.50", "BBQ Meatlovers":"$13.50", "Godfather":"$13.50"
    }
pprint.pprint(pizzaTypes)

#Output:
{'Apricot Chicken': '$13.50',
 'BBQ Meatlovers': '$13.50',
 'BBQ Pork & Onion': '$8.50',
 'Beef & Onion': '$8.50',
 'Cheesy Garlic': '$8.50',
 'Cranberry & Chicken': '$13.50',
 'Godfather': '$13.50',
 'Ham & Cheese': '$8.50',
 'Hawaiian': '$8.50',
 'Mr. Wedge': '$13.50',
 'Pepperoni': '$8.50',
 'Simply Cheese': '$8.50'}