如何在 Python 3 中的同一个键中附加多个值?
How to append multiple values in the same key in Python 3?
与我见过的例子不同,我认为我的例子有点不同,到目前为止还没有找到任何可以帮助我的东西。所以在这里我再次寻求帮助。现在已经为此工作了大约 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
要得到你想要的,你应该修改函数 createINV()
。确切地说,您应该修改存储数据的方式。
def createINV():
clear()
# one dictionary to store all the data
# keys are inventories
# values are sets of items
invdict = dict()
countinv = int(input("Enter the number of Inventories: "))
for a in range(countinv):
addinv = input("Enter Inventory #%d: " % (a+1))
# initialize key-value pairs
invdict[addinv] = set()
# iterate over inventories
for inv in invdict:
countitem = int(input("\nHow many items in %s inventory: " % inv))
for c in range(countitem):
additem = input("Enter item #%d: " % (c+1))
# add item to appropriate inventory
invdict[inv].add(additem)
print('\nYour inventory:')
print(invdict)
输出:
Enter the number of Inventories: 3
Enter Inventory #1: qwe
Enter Inventory #2: asd
Enter Inventory #3: zxc
How many items in qwe inventory: 1
Enter item #1: rty
How many items in asd inventory: 2
Enter item #1: fgh
Enter item #2: jkl
How many items in zxc inventory: 3
Enter item #1: vbn
Enter item #2: m,.
Enter item #3: ///
Your inventory:
{'qwe': {'rty'}, 'asd': {'jkl', 'fgh'}, 'zxc': {'vbn', 'm,.', '///'}}
与我见过的例子不同,我认为我的例子有点不同,到目前为止还没有找到任何可以帮助我的东西。所以在这里我再次寻求帮助。现在已经为此工作了大约 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
要得到你想要的,你应该修改函数 createINV()
。确切地说,您应该修改存储数据的方式。
def createINV():
clear()
# one dictionary to store all the data
# keys are inventories
# values are sets of items
invdict = dict()
countinv = int(input("Enter the number of Inventories: "))
for a in range(countinv):
addinv = input("Enter Inventory #%d: " % (a+1))
# initialize key-value pairs
invdict[addinv] = set()
# iterate over inventories
for inv in invdict:
countitem = int(input("\nHow many items in %s inventory: " % inv))
for c in range(countitem):
additem = input("Enter item #%d: " % (c+1))
# add item to appropriate inventory
invdict[inv].add(additem)
print('\nYour inventory:')
print(invdict)
输出:
Enter the number of Inventories: 3
Enter Inventory #1: qwe
Enter Inventory #2: asd
Enter Inventory #3: zxc
How many items in qwe inventory: 1
Enter item #1: rty
How many items in asd inventory: 2
Enter item #1: fgh
Enter item #2: jkl
How many items in zxc inventory: 3
Enter item #1: vbn
Enter item #2: m,.
Enter item #3: ///
Your inventory:
{'qwe': {'rty'}, 'asd': {'jkl', 'fgh'}, 'zxc': {'vbn', 'm,.', '///'}}