Searching Python dictionary for matching key

13,821

Solution 1

Since dictionaries are hashed tables, you can't just look them up by the last bit of the key like you would the whole key. Instead you have to iterate over the keys

for key in dictionary.keys():
    if key[-9:] == 'BSTTHGIHG':
        print('I found it!!')
        break

Note the use of key[-9:]

Solution 2

an element in the dictionary looks like {'key' , 'value'} keys are unique within dictionary while values may be not. assume your dictionary looks like

dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'};

so to get the value corresponding to any key , you can do so by :

print dict['Name'] # which will display 'Zara'

now regarding strings , if you have val = "ABCDEFGH" and you want to get substring from index 1 to the end you can do so by :

val = "ABCDEFGH"
print val[1:] # this will display "BCDEFGH"
print val[2:] # this will display "CDEFGH"
# A negative index accesses elements from the end of the list counting backwards.
print val[-1:] # this will display "H"
print val[-3:] # this will display "FGH" 

so for your example you want to search a dictionary for a key that matches exactly the last 9 letters of the string

st = "ABSTTHGIHG"
print dict[st[-9:]] # this will display the value corresponding to the key "BSTTHGIHG" if it exists

Solution 3

st = "ABSTTHGIHG"

for x in range(1,len(st)):
    for key in dictionary.keys():
        if st[x:] == key:
            #do something with your key
        print(st[x:])

that's how you can get last x letters of your stirng

out

BSTTHGIHG
STTHGIHG
TTHGIHG
THGIHG
HGIHG
GIHG
IHG
HG
G

so you have last 9 letters, 8, 7....

Share:
13,821
grindbert
Author by

grindbert

Just a regular clueless dude derping around with programming in his free time.

Updated on June 06, 2022

Comments

  • grindbert
    grindbert almost 2 years

    I want to search a python dictionary for a key that matches an initial sequence of letters. However, it's a bit more complicated and I lost myself midway in confusion.

    Example: I have a string "ABSTTHGIHG" and i want to search a dictionary for a key that matches exactly the last 9 letters of the string. If none is found i want to search another dictionary for a key that matches exactly the last 8 letters of the string.

    I have no idea how to match a specific number of letters (here "the last x letters") with a key (or how to search for a key) so i have hopes that somebody here can perhaps name a function that enables me to do so or where i can look it up. There is no code that i can present since this would be the first thing the program does. Can i specify "the last 9 letters" with something like x = string[1:]? That would be the only idea i have how to specify which letters of the string to use for the search.

    • Rahul Mohanraj
      Rahul Mohanraj almost 9 years
      Check this out stackoverflow.com/a/4843172/2182940 . Hope it helps :)
    • gabhijit
      gabhijit almost 9 years
      x = string[-9:] should be suffice. That's not working for you?
    • Ignacio Vazquez-Abrams
      Ignacio Vazquez-Abrams almost 9 years
      It might be worth creating a new type that matches on the last n characters if you need this to be efficient.
    • randomusername
      randomusername almost 9 years
      Is there anything else? Or are you ready to accept an answer?
    • grindbert
      grindbert almost 9 years
      i think all answers combined enable me to do what i want. i'll try it out.
  • grindbert
    grindbert almost 9 years
    it is indeed the whole key i want to match with the last x letters of the initial string.
  • grindbert
    grindbert almost 9 years
    every time i want to run it it shows "AttributeError: 'list' object has no attribute 'keys'". any idea why?
  • Aleksander Monk
    Aleksander Monk almost 9 years
    @grindbert Maybe you have there a list of a dictionaries
  • grindbert
    grindbert almost 9 years
    that is possible, i will check it out.