Python Forum

Full Version: Partial KEY search in dict
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all, i am trying to do a partial search in a dict, everything i have tried needs the exact search term that will find the value if it contains the search but what i need is the opposite, enter a partial search item that will find the value as long as the key contains the search term.

est_dict = {"G1": 3, "GROUP1": 3, "G2": 5, "GROUP2": 5, "G3": 7, "GROUP3": 7, "LR": 9, "LISTED": 9, "QUALITY": 10,
                 "OPEN": 12, "HCP": 12, "HANDICAP": 12, "100": 12, "96": 13, "95": 13, "94": 13, "93": 13, "92": 13,
                 "91": 13, "C6": 13, "CL6": 13, "CLASS6": 13, "3MW": 15, "2MWA": 15, "WELTER": 16, "FLYING": 16,
                 "90": 18,
                 "89": 18, "88": 18, "87": 18, "86": 18, "85": 18, "1MW": 18, "ONE METRO WIN": 18, "0MW": 18,
                 "NO METRO WIN": 18, "SET WEIGHTS": 20, "C5": 21, "CLASS5": 21, "84": 21, "83": 21, "82": 21, "81": 21,
                 "80": 21, "79": 21, "78": 24, "77": 24, "76": 24, "75": 24, "74": 24, "73": 24, "72": 24, "71": 24,
                 "C4": 25, "CLASS4": 25, "C3": 27, "CLASS3": 27, "70": 30, "69": 30, "68": 30, "67": 30, "66": 30,
                 "65": 30,
                 "64": 32, "63": 32, "62": 32, "61": 32, "60": 32, "59": 32, "C2": 34, "CLASS2": 34, "C1": 36,
                 "CLASS1": 36,
                 "58": 39, "57": 39, "56": 39, "55": 39, "54": 39, "53": 39, "52": 39, "51": 39, "50": 39,
                 "MAIDEN": 42, "MDN": 42, "MSW": 42}


search_key = "2MW"

res = [val for key, val in test_dict.items() if search_key in key]

print(str(res))
this code would finds "2MWA": 15 and print 15

but if i put a search_key of "M2MWA" it find nothing.
As a note the partial search could be anywhere with in the key.
As expected? "M2MWA" isn't found within any key of est_dict. Why are you expecting to find something?
Just trying to see if there is a solution, like a reverse search.
I might just have to build up a dict of all possible combinations.
I'm confused about what your question is. Can you show some examples of what you'd want to get given the dict and some keys?
(Mar-28-2023, 04:18 AM)klatlap Wrote: [ -> ]Hi all, i am trying to do a partial search in a dict,
[...]
As a note the partial search could be anywhere with in the key.
Hi!

1) I think your program works nicely. The only thing is that you missed the letter "t" on the first line at the beginning of your code in your post (it should be "test_dict" instead of "est_dict"):

test_dict = {"G1": 3, "GROUP1": 3, "G2": 5, "GROUP2": 5, "G3": 7, "GROUP3": 7, "LR": 9, "LISTED": 9, "QUALITY": 10,
                 "OPEN": 12, "HCP": 12, "HANDICAP": 12, "100": 12, "96": 13, "95": 13, "94": 13, "93": 13, "92": 13,
                 "91": 13, "C6": 13, "CL6": 13, "CLASS6": 13, "3MW": 15, "2MWA": 15, "WELTER": 16, "FLYING": 16,
                 "90": 18,
                 "89": 18, "88": 18, "87": 18, "86": 18, "85": 18, "1MW": 18, "ONE METRO WIN": 18, "0MW": 18,
                 "NO METRO WIN": 18, "SET WEIGHTS": 20, "C5": 21, "CLASS5": 21, "84": 21, "83": 21, "82": 21, "81": 21,
                 "80": 21, "79": 21, "78": 24, "77": 24, "76": 24, "75": 24, "74": 24, "73": 24, "72": 24, "71": 24,
                 "C4": 25, "CLASS4": 25, "C3": 27, "CLASS3": 27, "70": 30, "69": 30, "68": 30, "67": 30, "66": 30,
                 "65": 30,
                 "64": 32, "63": 32, "62": 32, "61": 32, "60": 32, "59": 32, "C2": 34, "CLASS2": 34, "C1": 36,
                 "CLASS1": 36,
                 "58": 39, "57": 39, "56": 39, "55": 39, "54": 39, "53": 39, "52": 39, "51": 39, "50": 39,
                 "MAIDEN": 42, "MDN": 42, "MSW": 42}
 
 
search_key = "2MW"
 
res = [val for key, val in test_dict.items() if search_key in key]
 
print(str(res))
then the output is:

[15]
corresponding to
test_dict = { [...] "2MWA" : 15, [...] }
2) If you change the original line:

search_key = "2MW"
to:

search_key = "METRO"
then the output is:

[18, 18]
corresponding to
test_dict = { [...]"ONE METRO WIN": 18, [...] "NO METRO WIN": 18, [...] }
3) If you change the original line:

search_key = "2MW"
to:

search_key = "LASS"
then the output is:

[13, 21, 25, 27, 34, 36]
corresponding to
test_dict = { [...] CLASS6": 13, [...] "CLASS5": 21, [...] "CLASS4": 25, [...] "CLASS3": 27, [...] "CLASS2": 34, [...] "CLASS1": 36, [...] }
I hope it helps,
Probably have a look at https://pypi.org/project/fuzzywuzzy/ - it may help in matching the keys