如何在Python 3的同一个键中附加多个值?

时间:2018-09-26 08:02:28

标签: python python-3.x dictionary append key-value

与我所看到的示例不同,我认为我的有所不同,到目前为止,还没有发现任何可以帮助我的东西。所以在这里我再次寻求帮助。现在已经进行了大约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

1 个答案:

答案 0 :(得分:1)

要获得所需的内容,您应该修改功能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,.', '///'}}