我有一个包含dict对象的列表,一个dict代表我的MySQL数据库中的一条记录 . 所以我想找到每个字典,其中 title
键的值是重复的,然后更新重复的项目 .
所以我有这样的事情:
dict1 = {"id":1234,"title":"apple"}
dict2 = {"id":1235,"title":"orange"}
dict3 = {"id":1236,"title":"apple"}
dict4 = {"id":1237,"title":"kiwi"}
results = [dict1,dict2,dict3,dict4]
我想最终得到这样的东西:
dict1 = {"id":1234,"title":"apple"}
dict2 = {"id":1235,"title":"orange"}
dict3 = {"id":1236,"title":"apple 2"}
dict4 = {"id":1237,"title":"kiwi"}
这是我的尝试,使用peewee并且我'm getting the base array from database, therefore I' m访问像 obj.title
这样的值,但我认为这些对象可以作为一个简单的dict处理)
objects = mydb.select().where(mydb.title.contains("usa"))
objectsB = objects
for obj in objects:
for obj2 in objectsB:
if obj.title is obj2.title and obj.objectId is not obj2.objectId:
obj2.title = "%s Duplicate" % (obj.title,)
print(obj2.title)
正如您所看到的,我有另一个密钥 objectId
,它始终不同,因此可以在for循环期间用于查找重复条目 . 但是我可能会做错了 .
2 回答
为什么不使用Peewee ORM SQL为您完成工作?
然后你将有一个列表,其中只包含多个具有相同 Headers 的水果 .
我使用
Counter
所以你只需要遍历结果集一次:哪个印刷品: