我有一个dicts列表,我想删除所有dicts,其中几个特定键的值小于另一个dict中的对应值:
这是我的代码:
for d in clean_hosting_dicts:
if (int(str(d["cores"]) ) < int(self.query["cores"]) ) or ( float(str(d["bandwidth"]).replace(",",".") ) < int(str(self.query["bandwidth"])) ) or ( float(str(d["storage"]).replace(",",".") ) < int(str(self.query["storage"])) ) or (float(str(d["ram"]).replace(",",".") ) < int(str(self.query["ram"])) ):
del clean_hosting_dicts[clean_hosting_dicts.index(d)]
clean_hosting_dicts
是我的dicts列表,而self.query
是另一个dict,它具有相同的键但其他值可以测试它们。
这里是自我的json:
{"hosting_type" : "vps","cores": "3", "storage" :"2", "bandwidth" : "1", "ram" : "2", "ip" : "1"}
在此代码运行之前,这里是用于dicts列表的json(未过滤列表):
[
{
"support_score": "4,5",
"name": "Hostgator Snappy 8000",
"ip": "2",
"support": "chat,email,docs,phone,ticket",
"ram": "8",
"storage": "240",
"frequency_rate": 3,
"cpu": "AMD Opteron 6378",
"reviews": "3",
"bandwidth": "3",
"domains": "1",
"cores": "4",
"os": "CentOS 66",
"price": "39,95",
"guarantee": "45"
},
{
"support_score": "3,5",
"name": "Bluehost Standard",
"ip": "1",
"support": "email,chat,doc,phone,ticket",
"ram": "2",
"storage": "30",
"frequency_rate": 3,
"cpu": "AMD Opteron 6378",
"reviews": "3,2",
"bandwidth": "1",
"domains": "1",
"cores": "2",
"os": "CentOS 66",
"price": "19,99",
"guarantee": "30"
},
{
"support_score": "3,5",
"name": "Bluehost Enhanced",
"ip": "2",
"support": "email,chat,doc,phone,ticket",
"ram": "4",
"storage": "60",
"frequency_rate": 3,
"cpu": "AMD Opteron 6378",
"reviews": "3,2",
"bandwidth": "2",
"domains": "1",
"cores": "2",
"os": "CentOS 66",
"price": "29,99",
"guarantee": "30"
},
{
"support_score": "3,5",
"name": "Bluehost Ultimate",
"ip": "2",
"support": "email,chat,doc,phone,ticket",
"ram": "8",
"storage": "240",
"frequency_rate": 3,
"cpu": "AMD Opteron 6378",
"reviews": "3,2",
"bandwidth": "4",
"domains": "1",
"cores": "4",
"os": "CentOS 66",
"price": "59,99",
"guarantee": "30"
},
{
"support_score": "4",
"name": "iPage Business",
"ip": "2",
"support": "email,chat,doc,phone,ticket",
"ram": "4",
"storage": "90",
"frequency_rate": 3,
"cpu": "Unknown",
"reviews": "3,7",
"bandwidth": "3",
"domains": "1",
"cores": "2",
"os": "CentOS 64",
"price": "47,99",
"guarantee": "30"
},
{
"support_score": "4,5",
"name": "InMotion vps-1000ha-s",
"ip": "3",
"support": "email, phone, skype, ticket,chat",
"ram": "4",
"storage": "75",
"frequency_rate": 3,
"cpu": "Unknown",
"reviews": "4,5",
"bandwidth": "4",
"domains": "1",
"cores": 5,
"os": "CentOS 66",
"price": "29,99",
"guarantee": "30"
},
{
"support_score": "4,5",
"name": "InMotion vps-3000ha-s",
"ip": "5",
"support": "email, phone, skype, ticket,chat",
"ram": "8",
"storage": "260",
"frequency_rate": 3,
"cpu": "Unknown",
"reviews": "4,5",
"bandwidth": "6",
"domains": "1",
"cores": 5,
"os": "CentOS 66",
"price": "74,99",
"guarantee": "30"
},
{
"support_score": "4",
"name": "Fatcow Business",
"ip": "2",
"support": "email,chat,doc,phone,ticket",
"ram": "4",
"storage": "90",
"frequency_rate": 3,
"cpu": "Unknown",
"reviews": "3,8",
"bandwidth": "3",
"domains": "1",
"cores": "2",
"os": "CentOS 64",
"price": "47,99",
"guarantee": "30"
},
{
"support_score": "4",
"name": "1and1 Cloud L",
"ip": "1",
"support": "email,doc,phone,ticket",
"ram": "2",
"storage": "80",
"frequency_rate": 3,
"cpu": "Intel Xeon",
"reviews": "3",
"bandwidth": "0",
"domains": "1",
"cores": "2",
"os": "CentOS 7",
"price": "14,99",
"guarantee": "30"
},
{
"support_score": "4,5",
"name": "Hostgator Snappy 2000",
"ip": "2",
"support": "chat,email,docs,phone,ticket",
"ram": "2",
"storage": "120",
"frequency_rate": 3,
"cpu": "AMD Opteron 6376",
"reviews": "3",
"bandwidth": "1,5",
"domains": "1",
"cores": "2",
"os": "CentOS 64",
"price": "19,95",
"guarantee": "45"
}
]
此代码运行后,删除所有不符合要求的词组,除了一个名称为1&amp; 1 Cloud L&#34; ,这个有&#34; 2&#34;作为核心的值,query["cores"]
是4,大于2(我将所有这些值转换为数字)
我不知道为什么它没有被删除,我无法通过pdb调试找到任何可以帮助在线的东西。
答案 0 :(得分:3)
问题是您正在从正在迭代的列表中删除项目,因此我建议在不同的列表上进行迭代,例如使用表达式:
for d in clean_hosting_dicts[:]:
...
所以你迭代一个相等的列表,虽然不是同一个对象,只删除原始列表。
希望这有帮助!
答案 1 :(得分:1)
列表推导可用于根据条件削减列表:
clean_hosting_dicts = [
d for d in clean_hosting_dicts
if not ((int(str(d["cores"])) < int(self.query["cores"])) or (
float(str(d["bandwidth"]).replace(",", ".")) < int(
str(self.query["bandwidth"]))) or (
float(str(d["storage"]).replace(",", ".")) < int(
str(self.query["storage"]))) or (
float(str(d["ram"]).replace(",", ".")) < int(str(self.query["ram"]))))
]
在OP的代码中,在迭代时从列表中删除项目时会出现问题。上述方法可以工作,因为构建了一个新列表,然后将其分配回原始名称。
注意(从评论中)这将构建一个全新的对象,因此如果有人对原始列表有引用,那么该列表将不会更新。