允许用户更改嵌套列表/字典值(Allowing User to alter Nested List/Dictionary Values)

编程入门 行业动态 更新时间:2024-10-25 06:21:02
允许用户更改嵌套列表/字典值(Allowing User to alter Nested List/Dictionary Values)

我有一个包含字典值的列表(整个程序从文本文件读取行,然后将信息存储到字典中,然后将字典数据的所有“行”存储到列表中。试图找出一种有效且功能强大的方法来允许用户在他们想要删除的数据集中输入一些值,并允许它这样做。到目前为止我所得到的是:

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):

我不知道如何正确配置,以便用户可以输入密钥字符串值,然后根据该列表从列表中删除该字典值......任何建议?

I have a list that is holding dictionary values (the whole program is reading in lines from a text file, then storing information into a dictionary and then storing all of the "rows" of dictionary data into a list. I am running into an issue trying to figure an effective and functional way to allow for a user to input some value in the dataset that they want to remove and to allow it to do so. What I've got so far is:

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

The main issue is the last block of code at the bottom - the section with variable:

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

When I've tested index or text entries it never gets past the if block:

if(question in lstTable == True):

And I cannot figure out how to configure things correctly so that the user can just enter the key string value and then delete that dictionary value altogether from the list based on that...Any suggestions?

最满意答案

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

首先,您的lstTable不包含字典的行,它包含整个字典,如下所示:[{}]

所以当你if question in lstTable:做如下if question in lstTable:你实际上正在比较一个字符串和一个字典,正如你所说的,它永远不会经过。

选项2工作的原因是,当你在开始时执行了lstTable.append(dictRow)时, lstTable实际上引用了dictRow而不是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添加/删除项目, lstTable会自动为您显示更新的dictRow 。

There is a few problems here, let me address them one by one:

First your lstTable does not contain rows of the dictionary, it contains the entire dictionary, like this: [{}]

So when you do the if question in lstTable: you are actually comparing a string with a dictionary, as you've said, it would never go through.

The reason why option 2 works is that when you did lstTable.append(dictRow) at the beginning, lstTable actually took a reference of dictRow instead of values in dictRow. So things in lstTable auto updates when you change anything in dictRow since it's just a reference.

I suggest the following for your option 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

As I said your lstTable only stores a reference to your dictRow, I would not worry about changing anything in lstTable. Once you add/remove items from dictRow, lstTable would display the updated dictRow for you automatically.

更多推荐

本文发布于:2023-08-04 08:48:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1413058.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套   字典   用户   列表   Allowing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!