Why is this if the condition works partially?

I have a list of dicts that I would like to remove all dicts, where the value of several specific keys is less than their counterpart in another dict:

Here is my code:

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)]

Where clean_hosting_dictsis my list of dicts, and self.queryis another dict that has the same key, but with different values ​​for testing against them.

Here's the json for self.query:

{"hosting_type" : "vps","cores": "3", "storage" :"2", "bandwidth" : "1", "ram" : "2", "ip" : "1"}

And here is the json for the dicts list before running this code (unfiltered list):

[
  {
    "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"
  }
]

After running this code, delete all dicts that do not meet the requirements, except for one “the one whose name has 1 & 1 Cloud L”, this has “2” as the value for the kernels, and query["cores"]4, which is more than two (I convert all these values ​​are numbers)

, , , ir pdb.

+4
2

, , , , , :

for d in clean_hosting_dicts[:]:
    ...

, , , .

, !

+3

:

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 . , , .

( ), , , - , .

+1

Source: https://habr.com/ru/post/1666303/


All Articles