[英]I want to to be able to print the name of the keys in the dictionary then the description, value and quantity
what i want the output to look like is this:我想要的输出是这样的:
IM. Iced Milo (S$1.5) Qty: 2
HM. Hot Milo (S$1.2) Qty: 20
IC. Iced Coffee (S$1.5) Qty: 2
HC. Hot Coffee (S$1.2) Qty: 0
1P. 100 Plus (S$1.1) Qty: 50
CC. Coca Cola (S$1.3) Qty: 50
Instead my output is like this:相反,我的输出是这样的:
None Iced Milo (S$1.5) Qty: 2
None Hot Milo (S$1.2) Qty: 20
None Iced Coffee (S$1.5) Qty: 2
None Hot Coffee (S$1.2) Qty: 0
None 100 Plus (S$1.1) Qty: 50
None Coca Cola (S$1.3) Qty: 50
Here is my code:这是我的代码:
nested_dictionary = {
"IM.": {
"description": "Iced Milo",
"price": "(S$1.5)",
"quantity": "%d" % (int(IMquantity)),
},
"HM.": {
"description": "Hot Milo",
"price": "(S$1.2)",
"quantity": "%d" % (int(HMquantiy)),
},
"IC.": {
"description": "Iced Coffee",
"price": "(S$1.5)",
"quantity": "%d" % (int(ICquantity)),
},
"HC.": {
"description": "Hot Coffee",
"price": "(S$1.2)",
"quantity": "%d" % (int(pprint)),
},
"1P.": {
"description": "100 Plus",
"price": "(S$1.1)",
"quantity": "%d" % (int(onePquantity)),
},
"CC.": {
"description": "Coca Cola",
"price": "(S$1.3)",
"quantity": "%d" % (int(CCquantity)),
},
}
for x in nested_dictionary:
print(nested_dictionary, end=" ")
print(nested_dictionary[x]["description"], end=" ")
print(nested_dictionary[x]["price"], end=" ")
if nested_dictionary[x]["quantity"] == 0:
print("Qty: ***out of stock***\n")
else:
print("Qty: ", nested_dictionary[x]["quantity"], "\n")
Here's a reformulation of your code to use a list of products (if you wanted, turning that into a dict-of-dicts is a single dictionary comprehension away: nested_dictionary = {p["code"]: p for p in products}
.这是您的代码的重新编写以使用产品列表(如果您愿意,将其转换为 dict-of-dicts 是一个单独的字典理解: nested_dictionary = {p["code"]: p for p in products}
。
In addition, we're using .format_map()
, which accepts a mapping (eg a dictionary) and formats a string using the given format string and data in that dict.另外,我们使用.format_map()
它接受一个映射(例如字典) 格式的字符串使用在字典给定格式的字符串和数据。
products = [
{
"code": "IM",
"description": "Iced Milo",
"price": 1.5,
"quantity": 2,
},
{
"code": "HM",
"description": "Hot Milo",
"price": 1.2,
"quantity": 20,
},
{
"code": "IC",
"description": "Iced Coffee",
"price": 1.5,
"quantity": 2,
},
{
"code": "HC",
"description": "Hot Coffee",
"price": 1.2,
"quantity": 0,
},
{
"code": "1P",
"description": "100 Plus",
"price": 1.1,
"quantity": 50,
},
{
"code": "CC",
"description": "Coca Cola",
"price": 1.3,
"quantity": 50,
},
]
for product in products:
line = "{code}. {description} (S${price:.2f}) Qty: {quantity}".format_map(product)
print(line)
The output is输出是
IM. Iced Milo (S$1.50) Qty: 2
HM. Hot Milo (S$1.20) Qty: 20
IC. Iced Coffee (S$1.50) Qty: 2
HC. Hot Coffee (S$1.20) Qty: 0
1P. 100 Plus (S$1.10) Qty: 50
CC. Coca Cola (S$1.30) Qty: 50
for x in nested_dictionary:
print(x, nested_dictionary[x]['description'], nested_dictionary[x]['price'], 'Qty:', end=' ')
if int(nested_dictionary[x]['quantity']) == 0:
print("***out of stock***")
else:
print(nested_dictionary[x]['quantity'])
You need to use the dict.items() to iterate over the dictionary Here is the code您需要使用 dict.items() 来遍历字典这里是代码
for (key,value) in nested_dictionary.items():
print(key,end=' ')
print(value['description'],end=' ')
print(value['price'],end=' ')
if (value['quantity'] == 0):
print("Qty: ***out of stock***\n")
else:
print(value['quantity']), "\n"
要打印键的名称,您必须在 for 循环中添加以下内容:
print(x, end=" ")
Trying to change your code as little as possible, here is the solution I got.尝试尽可能少地更改您的代码,这是我得到的解决方案。
nested_dictionary = {'IM.': {'description': 'Iced Milo', 'price': '(S$1.5)', 'quantity': int(IMquantity)},
'HM.': {'description': 'Hot Milo', 'price': '(S$1.2)', 'quantity': int(HMquantiy)},
'IC.': {'description': 'Iced Coffee', 'price': '(S$1.5)', 'quantity': int(ICquantity)},
'HC.': {'description': 'Hot Coffee', 'price': '(S$1.2)', 'quantity': int(pprint)},
'1P.': {'description': '100 Plus', 'price': '(S$1.1)', 'quantity': int(onePquantity)},
'CC.': {'description': 'Coca Cola', 'price': '(S$1.3)', 'quantity': int(CCquantity)}
for x in nested_dictionary:
print(x, end=' ')
print(nested_dictionary[x]['description'], end=' ')
print(nested_dictionary[x]['price'], end=' ')
if nested_dictionary[x]['quantity'] == 0:
print("Qty: ***out of stock***\n")
else:
print("Qty: ", nested_dictionary[x]['quantity'], "\n")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.