I am trying to find matching names in a dictionary. The user sends a request that looks like this:
req= [{"host":"usr1"}, {"host":"usr7"}, {"host":"usr10"}, {"host":"usr11"}, {"host":"usrx...."}, ..../more hosts]
Then I check for matches in another dictionary that looks like this:
data = [
{
"host": "usr1",
"address": "x"
},
{
"host": "usr2",
"address": "y"
},
{
"host": "usr3",
"address": "z"
}
.../ more hosts
]
if there is a match between the request from the user and the database I want to pop that host. I tried like this:
new_static = []
for x, i in zip(data, req):
if i['host'] != x['host']:
new_static.append(x)
data = new_static
continue
it works, but here a problem occurs if len of the req and the data is not equal, I also tried with double nested loops but with no success. Any help will be helpful
CodePudding user response:
You can try with the following nested loop solution:
req= [{"host":"usr1"}, {"host":"usr7"}, {"host":"usr10"}, {"host":"usr11"}]
data = [
{
"host": "usr1",
"address": "x"
},
{
"host": "usr2",
"address": "y"
},
{
"host": "usr3",
"address": "z"
}]
for i in req:
for j in data:
if i["host"] == j["host"]:
print("Host: ", i["host"])
Output:
Host: usr1
CodePudding user response:
Assuming the objects in your request only contain one key, you can simplify it to a set
for efficiency. Then, assuming you want to filter the elements of data
that have a host
not in req
, this can be accomplished by:
hosts_request = set(el['host'] for el in req)
filtered_data = [entry for entry in data if entry['host'] not in hosts_request]
CodePudding user response:
sinmple = [e["host"] for e in data]
for r in req:
if r["host"] in simple:
pass
# do something with the duplicat