[英]How to modify the values of a dictionary from a list of dictionaries
I am declaring a method named add_to_cart (db, itemid, quantity). 我在声明一个名为add_to_cart的方法(db,itemid,数量)。 Whenever the method is called, it will look into the database for the session data.
每当调用该方法时,它将在数据库中查找会话数据。 Session data contains a list of dictionaries.
会话数据包含词典列表。 The purpose of this method is to create a new entry (dictionary) to the list or update the value of existing entry.
此方法的目的是为列表创建一个新条目(字典)或更新现有条目的值。 Dictionary has the following keys: id, quantity
字典具有以下键:id,数量
So far I have developed the following code. 到目前为止,我已经开发了以下代码。 First of all after fetching the data from the database, I am matching the itemid with the dictionary key: 'id'.
首先,从数据库中获取数据后,我将itemid与字典键“ id”进行匹配。 If the itemid does not match with any of the values of the dictionaries, then it will append a new dictionary to that list.
如果itemid与字典的任何值都不匹配,则它将新字典添加到该列表。
def add_to_cart(db, itemid, quantity):
# ......
row = cursor.fetchone()
if row is not None:
cart = json.loads(row['data'])
for dic in cart:
if str(dic.get("id")) == str(itemid):
dic['quantity'] = int(dic['quantity']) + quantity
data = json.dumps(cart)
# update the 'data' to the database
break
else:
if counter == len(cart):
item = {
'id': itemid,
'quantity': quantity
}
cart.append(item)
data = json.dumps(cart)
# update the 'data' to the database
break
Let the initial cart is like: 让最初的购物车像:
[{'id': '40', 'quantity': '2'}, {'id': '41', 'quantity': '5'}]
[{'id':'40','quantity':'2'},{'id':'41','quantity':'5'}]
When I add 1 more of the item 40 to the cart, this should be like: 当我在购物车中再添加1件商品40时,应该像这样:
[{'id': '40', 'quantity': '3'}, {'id': '41', 'quantity': '5'}]
[{'id':'40','quantity':'3'},{'id':'41','quantity':'5'}]
but I am getting : 但我得到:
[{'id': '40', 'quantity': '2'}, {'id': '41', 'quantity': '5'}, {'id': '40', 'quantity': '1'}]
[{'id':'40','quantity':'2'},{'id':'41','quantity':'5'},{'id':'40','quantity': '1'}]
You are adding a new dictionary to the list when you do cart.append(item)
, hence the list 您在执行
cart.append(item)
时将新字典添加到列表中,因此该列表
[{'id': '40', 'quantity': '2'}, {'id': '41', 'quantity': '5'}]
ends up being 最终成为
[{'id': '40', 'quantity': '2'}, {'id': '41', 'quantity': '5'}, {'id': '40', 'quantity': '1'}]
But you want to find the matching id in that list of dictionaries, and add to the quantity for that dictionary. 但是您想在该词典列表中找到匹配的ID,然后将其添加到该词典的数量中。
So the code will look like as below: 因此,代码如下所示:
li = [{'id': '40', 'quantity': '2'}, {'id': '41', 'quantity': '5'}]
def add_elem(li, id, to_add):
#Iterate over the dictionaries
for item in li:
#If the id is found
if str(id) in item.values():
#Increment the quantity
item['quantity'] = str(int(item['quantity']) + to_add)
#Return the updated list
return li
print(add_elem(li, 40, 1))
The output will be 输出将是
[{'id': '40', 'quantity': '3'}, {'id': '41', 'quantity': '5'}]
The problem seems to be that you are going simply adding a new dict to the list (cart), through append
. 问题似乎是您将通过
append
将一个新的dict简单地添加到列表(购物车)中。 You need to go through the list, find the dict with the itemid
you need, then add to the quantity
. 您需要浏览列表,找到具有所需
itemid
的字典,然后将其添加到quantity
。
Try this - 尝试这个 -
for dict in cart:
if dict[itemid] == itemid:
dict['quantity'] += str(quantity)
break
item = {
'id': itemid,
'quantity': quantity
}
cart.append(item)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.