gpt4 book ai didi

python - 向python中预先存在的字典键添加一个值

转载 作者:太空宇宙 更新时间:2023-11-04 01:35:25 25 4
gpt4 key购买 nike

我一直在尝试为预先存在的字典键添加一个值。该值来自具有名为 price 的属性的 db.Model Car

尝试了两个代码块(如下),但得到了 AttributeError:“GqlQuery”对象没有属性“price”。

items = {"BE1234":"2","BE1923":"3","BE2032":"1", etc}
------------------------------------------------------------
block 1)
for item in items:
cars = db.GqlQuery('SELECT * FROM Car WHERE item=:1',item)
items[item] = cars.price
------------------------------------------------------------
block 2)
for item in items:
cars = db.GqlQuery('SELECT * FROM Car WHERE item=:1',item)
items.update({item:cars.price})

我想要得到的是:

items = {"BE1234":["2","100"],"BE1923":["31","200"],"BE2032":["19","300"]等

更新(最终工作代码)

for item in items:
cars = db.GqlQuery('SELECT * FROM Car WHERE item=:1',item)
for car in cars:
items[item]=[items[item],str(car.price)]

最佳答案

构造一个要插入的列表(值),只更新一个键的值:项目["BE1234"] = ["2", "100"]

详细(Shiva 的评论有一种“更干净”的方式来做同样的事情):

current_val = items["BE1234"]
new_val = "100";
new_array = [current_val, new_val]
items["BE1234"] = new_array

关于python - 向python中预先存在的字典键添加一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10370163/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com