mardi 30 juin 2015

Python - get object - Is dictionary or if statements faster?

I am making a POST to a python script, the POST has 2 parameters. Name and Location, and then it returns one string. My question is I am going to have 100's of these options, is it faster to do it in a dictionary like this:

myDictionary = {{"Name":{"Location":"result"},{"LocationB":"resultB"}},
        {"Name2":{"Location2":"result2A"},{"Location2B":"result2B"}}}

And then I would use.get("Name").get("Location") to get the results

OR do something like this:

if Name = "Name":
     if Location = "Location":
         result = "result"
     elif Location = "LocationB":
         result = "resultB"
elif Name = "Name2":
         if Location = "Location2B":
             result = "result2A"
         elif Location = "LocationB":
             result = "result2B"

Now if there are hundreds or thousands of these what is faster? Or is there a better way all together?

Aucun commentaire:

Enregistrer un commentaire