gpt4 book ai didi

python - For 语句在 Python 中遍历字典

转载 作者:行者123 更新时间:2023-11-28 22:41:25 25 4
gpt4 key购买 nike

我需要在字典上使用 for 循环来显示所有对应的值

shops = {

'Starbucks': {
'type':'Shops & Restaurants',
'location':'Track 19'
},

'Supply-Store': {
'type':'Services',
'location':'Central Station'
}
}

for shop in shops:
print(shop + " is a: " +shop.items(0))

我想让我的for循环做的是一次取一个项目,然后得到相应的类型和位置。现在,我一直在获取相应的类型和位置。

预期输出为:

Starbucks is a Shops & Restaurants located at Track 19.
Supply-Store is a Services located at Central Station.

最佳答案

假设您的 shops 字典中的每个值都是另一个具有类型和位置的字典。

你想要的可能是-

for key,value in shops.items():
print(key + " is a: " + value['type'] + " at : " + value['location'])

关于python - For 语句在 Python 中遍历字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32628642/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com