我有一个关于python 3.4.1的问题
假设您拥有一家商店,并且您拥有在商店中销售的所有商品的清单。比将项目分成类似项目的组。例如:洗发水,护发素和肥皂都属于同一组。现在,我要问的是当你在项目总列表中查找项目时,如何让它显示名称(我已经知道如何做)以及组中的其他项目。这就是我到目前为止所做的。
food = {"Chicken": "Chicken $4.99", "Beef": "Beef $5.95", "Taco": "Taco $3.50"}
toys = {"Blocks": "Blocks $12.35", "Army men": "Army men $5.99"}
#Ect.
search=input("Search Here ")
print(search)
谢谢你的时间!
James H。
答案 0 :(得分:2)
我建议使用字典,请参阅:
items = {'food': {"Chicken": "Chicken $4.99", "Beef": "Beef $5.95", "Taco": "Taco $3.50"},
'toys': {"Blocks": "Blocks $12.35", "Army men": "Army men $5.99"}}
search=input("Search Here ")
print(search)
for key in items:
if search in items[key]:
print(key)
print(items[key])
print.items[key].keys()
答案 1 :(得分:-3)
food = {"Chicken": "Chicken $4.99", "Beef": "Beef $5.95", "Taco": "Taco $3.50"}
toys = {"Blocks": "Blocks $12.35", "Army men": "Army men $5.99"}
#Ect.
search = input("Search Here ")
print(search)
if search in food: # If the key exists in the dict, print all items
print (food.items())
elif search in toys:
print (toys.items())
如果您只想要列表中的所有键:
if search in food:
print (food.keys()) # see just the keys
elif search in toys:
print (toys.keys())
food = ("Chicken", "Beef", "Taco")
toys = ("Blocks", "Army men")
search = input("Search Here ")
print(search)
if search in food:
print (food)
elif search in toys:
print (toys)
检查哪个元组搜索,然后打印该元组