6

Iterating over dict values - related to this question:

def bla(self,x,y)   
    for i in self.DataBase.keys():
        for x,y in self.DataBase[i]:
            if x == dept and y == year:
                return self.DataBase[i]

This is more of the idea that I am trying to achieve, how do I take a key and search for n values in the key, then return the key if the values are in the key

Community
  • 1
  • 1
Mike.G
  • 373
  • 2
  • 5
  • 11

3 Answers3

3

Below, the method bla returns the database key if x and y match the first and second elements of the tuple (of whatever length), respectively, that correspond to the key:

def bla(self, x, y)   
    for key, value in self.DataBase.iteritems():
        if (x, y) == value[:2]:
            return key

And now below, the method bla returns the database key if the database value which is a tuple contains both x and y:

def bla(self, x, y)   
    for key, value in self.DataBase.iteritems():
        if x in value and y in value:
            return key
dopstar
  • 1,478
  • 10
  • 20
2

Since you said "return the key" in the question, I assume you actually want to isolate the keys in the dictionary whose values match some set of search parameters (the code fragment you posted returns the values, not the keys). Assuming self.Database is a dictionary, you could extract the keys as a list comprehension with something like the following:

def getMatchingKeys(self, x, y):
  '''Extract keys whose values contain x and y (position does not matter)'''
  return [i for i in self.Database if x in self.Database[i] and y in self.Database[i]]

Keys whose values contain both x and y anywhere in the tuple will be returned. If you need to match specific positions in the tuple, the conditional inside the comprehension can be changed to something like if x == self.Database[i][1] and y == self.Database[i][2].

If you aren't after the keys, please clarify your question.

rchang
  • 5,150
  • 1
  • 15
  • 25
  • ill try to explane my best, i have a func that gets self,x,y what it needs to do is to search in each key that is the the database (dictionary database) for the x and y if x and y in database[key] return the key i hope thats more understandable and the tuples are more then size 2, they are 4 or to be even more acurrate, to search for n values in a key. – Mike.G Jan 01 '15 at 20:57
  • @Mike.G That does help clarify a bit - I've edited my answer accordingly. As I mention in the answer, the exact implementation will differ depending on whether you intend to search for x and y anywhere in the tuple or if you are looking in specific positions. – rchang Jan 01 '15 at 21:03
  • i belive that, that would be in specific positions that the values will be sense the entering order is the same for each key. i mean the key that holds the values its allways will be the same positions for each value. – Mike.G Jan 01 '15 at 21:05
1

You may use the built-in map() to set the x and y variables.

def bla(self,x,y) :
    for key in self.DataBase :
        x,y = map(float, self.DataBase[key])
        if x == dept and y == year:
            return key

If you prefer using items(), you may do the following as well (equally valid):

def bla(self,x,y):
    for key, val in self.DataBase.items():
        x, y = map(float, val)
        if x == dept and y == year:
            return key

Here's yet another way of doing it without map(), this gives you the advantage of unpacking the tuples while iterating over the dict:

 def bla(self,x,y):
    for key, (x, y) in self.DataBase.items():
        if x == dept and y == year:
            return key

You may also write the above as so, using list comprehension, although I would say the one above is preferable:

def bla(self,x,y):
    found = {key for key, (x, y) in self.DataBase.items() if x==dept and y==year}
    found = ''.join(num) #joins set into string
    return found

The following all work for Python 3, which I assume is what you want since one of your tags are Python 3.x

buydadip
  • 8,890
  • 22
  • 79
  • 154
  • 1
    Using `map` is unnecessary as there is no reason for the `bla` method to impose type conversion of its arguments to float. In all your code snippets, arguments `x` and `y` are shadowed by the re-assignment step which give them self.DataBase values.There is also an unpacking error vulnerability since there is an assumption that the self.DataBase values will be tuples or lists of exactly length 2 (which the OP stated that its the case). I think `depth` and `year` were meant to be the arguments not globals. Otherwise there is no need for the arguments on the `bla` method signature. – dopstar Jan 04 '15 at 14:35