我有一个dict列表,我想删除一些特定键的值小于另一个dict中对应键的所有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
是我的dict列表,self.query
是另一个dict,它有相同的键,但有其他值要对它们进行测试。你知道吗
以下是用于自我查询地址:
{"hosting_type" : "vps","cores": "3", "storage" :"2", "bandwidth" : "1", "ram" : "2", "ip" : "1"}
下面是代码运行前dict列表的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"
}
]
在这个代码运行之后,删除所有不符合要求的dict,除了一个“名称为1&1 Cloud L的dict”,这个dict的核心值是“2”,而query["cores"]
是大于2的4(我将所有这些值转换成数字)
我不知道为什么它没有被删除,我找不到任何有助于在线红外调试通过pdb。你知道吗
列表理解可用于根据条件对列表进行缩减:
在OP的代码中,问题出现在从列表中删除迭代时。上面的方法可以工作,因为建立了一个新的列表,然后将其分配回原始名称。你知道吗
注意(从注释中)这将构建一个全新的对象,因此如果有人引用了原始列表,那么该列表将不会被更新。你知道吗
问题是您正在从正在迭代的列表中删除项,因此我建议您在其他列表上迭代,例如使用以下表达式:
所以你在一个相等的列表上迭代,虽然不是同一个对象,但是只在原始的列表中删除。你知道吗
希望这有帮助!你知道吗
相关问题 更多 >
编程相关推荐