如何附加到由另一个键标识的列表中的字典中的列表?

2024-09-27 09:32:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个字典列表,其中一个键代表一个名字,一个键代表一个项目列表(除其他键外)。我有一个参数为name和item的函数。检查字典以查看名称是否匹配,如果项与预定集匹配,则它将成为MainItem。否则,如果它与misc的另一个预定集匹配。项目,它将被附加到“其他项目”列表中

目前,我的代码似乎附加了这些杂项。作为所有词典最后输出的项,所有列出的词典都具有相同的其他项列表。更新MainItem似乎很好,但我无法让其他项目按预期运行。下面是一些例子

personList = []
personStats = {"Name": "", "MainItem": "", "OtherItems": []}

修改后的名称personStats的唯一副本将附加到personList,如上所述,用于每个人,并按预期工作

def updatePersonItem(name, item):
    itemType = verifyPersonItem(item)
    if itemType == "Main":
        for person in personList:
            if person["Name"] == name:
                person["MainItem"] = name  
    elif itemType == "Misc":
        for person in personList:
            if person["Name"] == name:
                if item not in personStats["OtherItems"]:
                    person["OtherItems"].append(item)

verifyPersonItem()将该项与两个不同的集合进行比较,并返回Main或Misc。我还编写了updatePersonItem()的其他迭代,但最终仍然得到相同的结果

虽然它看起来不错,但运行上面的代码并打印personList输出的结果与下面类似

{"Name": "Bob", "MainItem": "Bag", "OtherItems": ['apple', 'banana', 'mandarin', 'orange', 'pear']}
{"Name": "Alice", "MainItem": "Lunchbox", "OtherItems": ['apple', 'banana', 'mandarin', 'orange', 'pear']}

但是,我希望得到如下输出,其中仅按照预期添加适用的项目

{"Name": "Bob", "MainItem": "Bag", "OtherItems": ['apple', 'orange', 'pear']}
{"Name": "Alice", "MainItem": "Lunchbox", "OtherItems": ['apple', 'mandarin']

有什么想法吗

编辑:我已经找到了一个解决方案,它似乎可以按预期的方式运行。见下文

def updatePersonItem(name, item):
    itemType = verifyPersonItem(item)
    for person in personList:
        if person["Name"] == name: 
            if itemType == "Main":
                person["Main"] = item     
            elif itemType == "Misc":
                if item not in person["OtherItems"]:
                    personOtherItemCopy = person["OtherItems"].copy()
                    personOtherItemCopy.append(item)
                    person["Misc"] = personOtherItemCopy

Edit2:我已经添加了一个我的原始代码的最小可复制示例,以根据要求模拟我的原始场景。希望这能带来更好的解决方案

import re 

personList = []
personStats = {"Name": "", "MainItem": "", "OtherItems": []}

def addPerson(name):
    if not any(d["Name"] == name for d in personList):
        personStatsCopy = personStats.copy()
        personStatsCopy.update({"Name": name})
        personList.append(personStatsCopy)
    else:
        updatePlayerClass(name, item)

def verifyPersonItem(item):
    verifyItemPattern = re.compile(r'(Lunchbox|Bag|Bento|Apple|Orange|Mandarin|Banana|Pear)')
    classMatch = re.search(verifyItemPattern, item)
    boolean = bool(classMatch)
    if boolean == True:
        if item == "Lunchbox" or item == "Bag" or item == "Bento":
            return ("Main")
        else:
            return ("Misc")
    else:
        print("Invalid item.")

def updatePersonItem(name, item):
    itemType = verifyPersonItem(item)
    if itemType == "Main":
        for person in personList:
            if person["Name"] == name:
                person["MainItem"] = item  
    elif itemType == "Misc":
        for person in personList:
            if person["Name"] == name:
                if item not in person["OtherItems"]:
                    person["OtherItems"].append(item)

addPerson("Bob")
addPerson("Alice")
updatePersonItem("Bob", "Bag")
updatePersonItem("Bob", "Apple")
updatePersonItem("Bob", "Orange")
updatePersonItem("Bob", "Mandarin")
updatePersonItem("Alice", "Mandarin")
updatePersonItem("Alice", "Lunchbox")
updatePersonItem("Alice", "Pear")

print(personList)

Tags: nameinforifmainitempersonmisc

热门问题