简体   繁体   English

如何编辑字典列表中的特定字典键值?

[英]How to Edit Specific Dictionary Key Values in List of Dictionaries?

I have a list of dictionaries like so, named result我有一个像这样的字典列表,命名为result

{'Item #': 2, 'Price ': 2.99, 'Quantity': 2, 'Name': 'Muffin2'}
{'Item #': 3, 'Price ': 3.99, 'Quantity': 3, 'Name': 'Cake1'}
{'Item #': 4, 'Price ': 4.99, 'Quantity': 4, 'Name': 'Cake2'}

I am trying to develop methods that upon user input, can edit the key values of a specific dictionary in the list.我正在尝试开发在用户输入时可以编辑列表中特定字典的键值的方法。 I want to traverse the dictionary, find the dictionary that has the Item # key value equal to the input, and then allow the user to edit the price, quantity, and name key values of the dictionary that matches their input.我想遍历字典,找到 Item # 键值等于输入的字典,然后允许用户编辑匹配其输入的字典的价格、数量和名称键值。

I was wondering what is the best way to do this?我想知道最好的方法是什么?

I was able to come with something like this for an idea, but wasn't sure if this was on the right path or idea wise?我能够提出这样的想法,但不确定这是在正确的道路上还是明智的想法?


 def enter_data(self, message, typ):
            while True:
                try:
                    v = typ(input(message))
                except ValueError:
                    print(f"Thats not an {typ}!")
                    continue
                else:
                    break
            return v

  def edit_item_interaction(self):
            self.database.show_available()
            edit_specific_item = self.enter_data("Enter the item # of the item you would like edit\n" , int)
            for item in self.database.result:
                if item["Item #"] == edit_specific_item:
                    index = #Index of dictionary in list where it was found?
                    break
            break
        float(change_price = self.enter_data("Enter the change in price\n" , float))
        self.databse.result[index]["Price "].append(change_price)
        self.database.result[index]["Price "].pop(1)

How do I keep track of the index where the dictionary is in the list, that matches the key value I am looking for?如何跟踪列表中字典所在的索引,与我要查找的键值匹配?

For example, If a user inputs they would like to edit item # 2, it should traverse the list of dicts to find the dictionary where the item number is, get the index of where this dictionary is in the list, and the use those two variables to change the Price Value Key for that dictionary, and pop out the old value.例如,如果用户输入他们想要编辑项目#2,它应该遍历字典列表以找到项目号所在的字典,获取该字典在列表中的位置的索引,并使用这两个变量来更改该字典的价格值键,并弹出旧值。

Am I on the right track?我在正确的轨道上吗?

It's rather simpler than this to find the dictionary that matches a specific 'Item #' key value, using next .使用next查找匹配特定'Item #'键值的字典比这要简单得多。

>>> result = [
...   {'Item #': 2, 'Price ': 2.99, 'Quantity': 2, 'Name': 'Muffin2'},
...   {'Item #': 3, 'Price ': 3.99, 'Quantity': 3, 'Name': 'Cake1'},
...   {'Item #': 4, 'Price ': 4.99, 'Quantity': 4, 'Name': 'Cake2'}
... ]
>>> next((d for d in result if d['Item #'] == 3), None)
{'Item #': 3, 'Price ': 3.99, 'Quantity': 3, 'Name': 'Cake1'}

Having found that, you simply need to modify it as you see fit.发现这一点后,您只需要根据需要对其进行修改。 For demonstration's sake, changing the price to 67 .为了演示,将价格更改为67

>>> d = next((d for d in result if d['Item #'] == 3), None)
>>> d['Price '] = 67
>>> result
[{'Item #': 2, 'Price ': 2.99, 'Quantity': 2, 'Name': 'Muffin2'}, {'Item #': 3, 'Price ': 67, 'Quantity': 3, 'Name': 'Cake1'}, {'Item #': 4, 'Price ': 4.99, 'Quantity': 4, 'Name': 'Cake2'}]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM