Python Forum
Fast method of searching a string in a multi-dimension array?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Fast method of searching a string in a multi-dimension array?
#1
Good day. Anyone, please suggest a quicker method to search a string in a multi-dimension array. Given the example below, I want to check if the word 'house' exist in the first column and get the ID beside it. I tried looping and it works, however, as the dictionary adds more words, it takes some time to check if an input string exist and take it's corresponding id. I already have 200K of words in my dictionary, so I need a much faster method.

dictionary = [['dog', 34], ['cat', 2], ['house', 5], ['big', 10], ['bed', 21]]

aim : check if a word exist in the dictionary and retrieve the ID.


Thank you in advance. Your help will be much appreciated. God bless.
Reply
#2
Best method for both ease of use and performance: use a real python dictionary

dictionary = {'dog': 34, 'cat':2, 'house': 5, 'big': 10, 'bed': 21}
id=dictionary['cat']
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#3
(Feb-20-2017, 11:55 AM)Ofnuts Wrote: Best method for both ease of use and performance: use a real python dictionary
 dictionary = {'dog': 34, 'cat':2, 'house': 5, 'big': 10, 'bed': 21} id=dictionary['cat'] 

Thank you for the response sir. I'll try your suggestions. Thank you very much. God bless you always.
Reply
#4
Can fix it so the variable name is right Wink
>>> dictionary = [['dog', 34], ['cat', 2], ['house', 5], ['big', 10], ['bed', 21]]
>>> dictionary = {k[0]: k[1] for k in dictionary}
>>> dictionary
{'dog': 34, 'cat': 2, 'house': 5, 'big': 10, 'bed': 21}
>>> type(dictionary)
<class 'dict'>
Now it's a dictionary,and get() is a good way to check if something is in dict.
>>> dictionary.get('house', 'Not in dict')
5
>>> dictionary.get('hello', 'Not in dict')
'Not in dict'
id is a used Python name Ofnuts.
Reply
#5
(Feb-20-2017, 12:16 PM)snippsat Wrote: Can fix it so the variable name is right Wink
>>> dictionary = [['dog', 34], ['cat', 2], ['house', 5], ['big', 10], ['bed', 21]] >>> dictionary = {k[0]: k[1] for k in dictionary} >>> dictionary {'dog': 34, 'cat': 2, 'house': 5, 'big': 10, 'bed': 21} >>> type(dictionary) <class 'dict'>
Now it's a dictionary,and get() is a good way to check if something is in dict.
>>> dictionary.get('house', 'Not in dict') 5 >>> dictionary.get('hello', 'Not in dict') 'Not in dict'
id is a used Python name Ofnuts.

Thank you so much for all of your replies. Great help from this community. God bless you as always.
Reply
#6
or just
>>> dict(dictionary)
Output:
{'bed': 21, 'house': 5, 'dog': 34, 'big': 10, 'cat': 2}
Reply
#7
(Feb-20-2017, 12:16 PM)snippsat Wrote: id is a used Python name Ofnuts.

Good to know... and useful...
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  splitting file into multiple files by searching for string AlphaInc 2 816 Jul-01-2023, 10:35 PM
Last Post: Pedroski55
  Array dimension don't match asja2010 0 1,019 Feb-23-2023, 04:22 PM
Last Post: asja2010
  x and y must have same first dimension, but have shapes (1,) and (50,) asja2010 5 2,481 Jan-12-2023, 07:24 PM
Last Post: deanhystad
  Strange error ValueError: dimension mismatch Anldra12 0 1,937 Aug-17-2021, 07:54 AM
Last Post: Anldra12
  ValueError: dimension mismatch Anldra12 0 3,345 Jul-17-2021, 04:46 PM
Last Post: Anldra12
  Convert String of an int array to a Numpy array of ints mdsousa 5 5,578 Apr-08-2021, 08:00 PM
Last Post: mdsousa
  ValueError: x and y must have same first dimension, but have shapes (11,) and (15406, hobbyist 17 149,016 Mar-22-2021, 10:27 AM
Last Post: hobbyist
  Error When Plotting ValueError: x and y must have same first dimension JoeDainton123 1 9,016 Oct-04-2020, 12:58 PM
Last Post: scidam
  Multi set string inputs/outputs kwmcgreal 2 2,010 Sep-26-2020, 10:44 PM
Last Post: kwmcgreal
  Create array from string steve87bg 4 3,115 Jul-13-2020, 07:55 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

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