In PHP, I had this line matches = preg_grep('/^for/', array_keys($hash));
What it would do is it would grab the words: fork, form etc. that are in $hash.
In Python, I have a dict with 400,000 words. It's keys are words I'd like to present in an auto-complete like feature (the values in this case are meaningless). How would I be able to return the keys from my dictionary that match the input?
For example (as used earlier), if I have
my_dic = t{"fork" : True, "form" : True, "fold" : True, "fame" : True}
and I get some input "for"
, It'll return a list of "fork"
, "form"
.
>>> mydict={"fork" : True, "form" : True, "fold" : True, "fame" : True}
>>> [k for k in mydict if k.startswith("for")]
['fork', 'form']
This should be faster than using a regular expression (and sufficient if you're just looking for word beginnings).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With