Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Partial KEY search in dict
#1
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.
Reply
#2
As expected? "M2MWA" isn't found within any key of est_dict. Why are you expecting to find something?
Reply
#3
Just trying to see if there is a solution, like a reverse search.
Reply
#4
I might just have to build up a dict of all possible combinations.
Reply
#5
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?
Reply
#6
(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,
Gribouillis likes this post
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply
#7
Probably have a look at https://pypi.org/project/fuzzywuzzy/ - it may help in matching the keys
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  partial functions before knowing the values mikisDeWitte 4 617 Dec-24-2023, 10:00 AM
Last Post: perfringo
  Move Files based on partial Match mohamedsalih12 2 833 Sep-20-2023, 07:38 PM
Last Post: snippsat
  search in dict inside tuple steg 1 711 Mar-29-2023, 01:15 PM
Last Post: rob101
  remove partial duplicates from csv ledgreve 0 796 Dec-12-2022, 04:21 PM
Last Post: ledgreve
  Webhook, post_data, GPIO partial changes DigitalID 2 997 Nov-10-2022, 09:50 PM
Last Post: deanhystad
  Optimal way to search partial correspondence in a large dict genny92c 0 1,005 Apr-22-2022, 10:20 AM
Last Post: genny92c
  Partial Matching Rows In Pandas DataFrame Query eddywinch82 1 2,383 Jul-08-2021, 06:32 PM
Last Post: eddywinch82
  Partial key lookup in dictionary GaryNR 1 3,461 Jul-16-2020, 06:55 PM
Last Post: Gribouillis
  Partial using Tkinter function chesschaser 10 6,826 Jul-03-2020, 03:57 PM
Last Post: chesschaser
  Partial Word Search Kristenl2784 2 2,132 Jun-29-2020, 08:26 PM
Last Post: Kristenl2784

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020