【发布时间】:2019-03-01 22:37:06
【问题描述】:
与我见过的例子不同,我认为我的例子有点不同,到目前为止还没有找到任何可以帮助我的例子。所以在这里我再次寻求帮助。现在已经为此工作了大约 3 天,我在 Python 3 中有点新,所以请多多包涵。谢谢。
到目前为止,我得到的字典是这样的:
{0: 'fruits', 1: 'veggies', 2: 'drinks'}
我希望是这样的:
{'fruits' : { 'apple', 'orange'}, 'veggies' : { 'cucumber','eggplant'}, 'drinks' : {'coke','juice'}}
我一直在尝试将其他(或多个)值附加到同一个键,但没有任何效果。似乎很难将值附加到字典中的键。在我不断尝试的同时,我还不如在线寻求帮助。
这是我的代码:
# MODULES
import os
# FUNCTIONS
def clear():
os.system('cls')
def skip():
input("<Press Enter To Continue>")
def createINV():
clear()
invdict = {}
invname = {}
countinv = int(input("Enter the number of Inventories: "))
for a in range(countinv):
# ADD LISTS HERE
addinv = str(input("Enter Inventory #%d: " % (a+1)))
invdict[a] = addinv
print(invdict)
for b in range(countinv):
countitem = int(input("\nHow many items in %r inventory: " % list(invdict.values())[b]))
for c in range(countitem):
additem = input("Enter item #%d: " % (c+1))
#invdict[c].extend
#list(invdict.keys(c)[]).append(additem)
#invdict.setdefault(c, []).append(c[additem])
#invdict[c].append(additem)
# d.setdefault(year, []).append(value)
for aprint in range(countinv):
for x,y in invdict.items():
print (x,y)
# for bprint in range(countitem):
# for y invname.value[bprint]:
# print(y)
# START - Welcome
clear()
print("Hi! Welcome to Python Inventory System.")
skip()
clear()
# START - Introduction
print("This is an Inventory System where you can input any Inventoriesyou want and how many you want.")
print("For e.g.: You can input 3 types of Inventories such as Vegetables, Fast Foods, Drinks, etc.")
print("And you can input the prices for each item.")
skip()
clear()
# COMMENCE PROGRAM
x = 0
while x != 1:
start = input("Are you ready to Start? (Y/N):")
if start == 'y' or start == 'Y':
x += 1
createINV()
elif start == 'n' or start == 'N':
x += 1
clear()
else:
x = 0
【问题讨论】:
-
查看
collections.defaultdict(set) -
也许您可以使用
dictionary和set作为值
标签: python python-3.x dictionary append key-value