Skip to main content
 首页 » 编程设计

python之有没有办法在每一行中打印一个字典的键和值

2025年05月04日13xing901022

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

我试过使用/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" 
    } 

以上是我的字典。谢谢

请您参考如下方法:

是的,确实有。

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

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

BBQ Pork & Onion $8.50 
Apricot Chicken $13.50 
Ham & Cheese $8.50 
Mr. Wedge $13.50 
Hawaiian $8.50 
BBQ Meatlovers $13.50 
Pepperoni $8.50 
Beef & Onion $8.50 
Godfather $13.50 
Cheesy Garlic $8.50 
Simply Cheese $8.50 
Cranberry & Chicken $13.50