允许用户更改嵌套 List/Dictionary 值

Allowing User to alter Nested List/Dictionary Values

我有一个包含字典值的列表(整个程序从文本文件中读取行,然后将信息存储到字典中,然后将字典数据的所有 "rows" 存储到列表中. 我 运行 遇到了一个问题,试图找出一种有效且实用的方法来允许用户在他们想要删除的数据集中输入一些值并允许它这样做。我得到了什么远是:

file = open('FOLDER\FILE.txt', "r")
strData = ""
dictRow = {}
lstTable = []

for line in file:
    k, v = line.strip().split(',')
    dictRow[k] = v.strip()

lstTable.append(dictRow)

print(lstTable)

file.close()


while(True):
    print ("""
    Menu of Options
    1) Show current data
    2) Add a new item.
    3) Remove an existing item.
""")

strChoice = str(input("Which option would you like to perform? [1 to 4] - "))
print()#adding a new line


if (strChoice.strip() == '1'):
    print("Current values in the list table are: ")
    print(lstTable)
    continue

elif(strChoice.strip() == '2'):

    newRow = {}     # Empty dictionary to hold new entries
    key = input("Enter a task to complete: ")
    val = input("Enter the priority of the task (high/average/low):  ")
    newRow[key.strip()] = val.strip()  # Formatting user input as dictionary values within newRow{}
    dictRow.update(newRow)     # Updates existing dictionary with new values (e.g. updates list/Table)

    print("The values in the list table are now: ")     # Display the results
    print(lstTable)
    continue


elif(strChoice == '3'):
    print(lstTable)
    question = input("What item do you want to remove from the list? ")

    if(question in lstTable == True):

        areYouSure = input("You selected " + question + " to be removed from the list. Continue? Y/N: ").upper()

        if(areYouSure == "Y"):
            lstTable.remove(question)
            print("You subtracted " + question + " from the list!")
            print("The values in the list table are now: ")
            print(lstTable)
        continue

主要问题是底部的最后一段代码 - 带有变量的部分:

question = input("What item do you want to remove from the list? ")

当我测试索引或文本条目时,它永远不会通过 if 块:

if(question in lstTable == True):

而且我不知道如何正确配置,以便用户只需输入键字符串值,然后根据该值从列表中完全删除该字典值...有什么建议吗?

而不是:

if(question in lstTable == True):

使用:

if question in lstTable:

Python 有运算符链接,所以第一个表达式没有按照您的预期进行。

这里有几个问题,让我一一解决:

首先你的 lstTable 不包含字典的行,它包含整个字典,像这样:[{}]

因此,当您执行 if question in lstTable: 时,您实际上是在将字符串与字典进行比较,正如您所说,它永远不会通过。

选项 2 起作用的原因是,当您在开始时执行 lstTable.append(dictRow) 时,lstTable 实际上引用了 dictRow 而不是 dictRow 中的值。因此,当您更改 dictRow 中的任何内容时,lstTable 中的内容会自动更新,因为它只是一个参考。

对于选项 3,我建议如下:

elif(strChoice == '3'):
    print(lstTable)
    question = input("What item do you want to remove from the list? ")
    if question in dictRow:
        areYouSure = input("You selected " + question + " to be removed from the list. Continue? Y/N: ").upper()    
        if(areYouSure == "Y"):
            del(dictRow[question])
            print("You subtracted " + question + " from the list!")
            print("The values in the list table are now: ")
            print(lstTable)
        continue

正如我所说,您的 lstTable 仅存储对您的 dictRow 的引用,我不担心更改 lstTable 中的任何内容。一旦您 dictRow 中的 add/remove 项,lstTable 将自动为您显示更新后的 dictRow