kdt 50
New Member
Wow, I really am full of horrid questions today.
I have a dictionary with a list of patterns: - >>> words = {'sho.':6, '.ilk':8,'.an.':78 }
Where the "." character means any pattern - this can easily be changed to the "*" symbol if need be.
When the user submits a word, I want to be able to look for a corresponding pattern (if it exists). For example if the user said "show" or "shoe", then the value 6 would be returned. If it was "band", "land", "sand", "pant" etc then 78 would be returned - but not "pants" as it is longer than the pattern.
I know the normal way is to provide the reg exp and search the dictionary with it, but this is the other way round :(
Thanks
4 27248 bartonc 6,596
Recognized Expert Expert
Wow, I really am full of horrid questions today.
I have a dictionary with a list of patterns: - >>> words = {'sho.':6, '.ilk':8,'.an.':78 }
Where the "." character means any pattern - this can easily be changed to the "*" symbol if need be.
When the user submits a word, I want to be able to look for a corresponding pattern (if it exists). For example if the user said "show" or "shoe", then the value 6 would be returned. If it was "band", "land", "sand", "pant" etc then 78 would be returned - but not "pants" as it is longer than the pattern.
I know the normal way is to provide the reg exp and search the dictionary with it, but this is the other way round :(
Thanks
The '$' is the "end of string" operator: -
>>> import re
-
>>> reObj = re.compile('.an.$')
-
>>> bool(reObj.match("pants"))
-
False
-
>>> bool(reObj.match("pant"))
-
True
-
>>>
-
>>> words = ["show", "shoe", "band", "land", "sand", "pant", "pants"]
-
>>> pattDict= {'sho.$':6, '.ilk$':8,'.an.$':78 }
-
>>> for word in words:
-
... for k, v in pattDict.items():
-
... if re.match(k,word):
-
... print word, v
-
...
-
show 6
-
shoe 6
-
band 78
-
land 78
-
sand 78
-
pant 78
-
>>>
Hope that helps.
kdt 50
New Member
It sickens me that you make it look so easy! This problem has been the bottleneck for two projects. Thank you so much :)
bartonc 6,596
Recognized Expert Expert
It sickens me that you make it look so easy! This problem has been the bottleneck for two projects. Thank you so much :)
<Which part: The k, v thing or the $ thing?>
Cheer up... I actually struggled with the $ thing for a bit.
kdt 50
New Member
Thankfully just the $ thing. :)
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Erik Lechak |
last post by:
Hello all,
I wrote the code below. It is simply a dictionary that uses regular
expressions to match keys. A quick look at _test() will give you an
example.
Is there a module that already...
|
by: Kenneth McDonald |
last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate
feedback, suggestions, and criticism as I work towards finalizing the
API and feature sets. rex is a module intended to make...
|
by: Harry |
last post by:
Hi there,
does anyone know how I can build a regular expression e.g. for the
string.search() function on runtime, depending on the content of
variables? Should be something like this:
var...
|
by: JohnSouth |
last post by:
Hi
I've been using a Regular expression to test for valid email addresses.
It looks like:
\w+(\w+)*@\w+(\w+)*\.\w+(\w+)*
I've now had 2 occassions where it has rejected and email address...
|
by: Ludwig |
last post by:
Hi,
i'm using the regular expression \b\w to find the beginning of a word,
in my C# application. If the word is 'public', for example, it works.
However, if the word is '<public', it does not...
| |
by: Iasthaai |
last post by:
Hello everyone, I'm new to Python and also new to this forum and I have a question regarding regular expressions, so here goes:
I am attempting to read in a text and do some pattern matching...
|
by: jlm699 |
last post by:
I have an application that is build on wxPython and have run into a small but annoying problem. I use a Search Control in my toolbar just as in the ToolBar example. Now when the user enters say...
|
by: NvrBst |
last post by:
I want to use the .replace() method with the regular expression /^ %VAR
% =,($|&)/. The following DOESN'T replace the "^default.aspx=,($|&)"
regular expression with "":...
|
by: micron_make |
last post by:
I am trying to parse a file whose contents are :
parameter=current
max=5A
min=2A
for a single line I used
for line in file:
print re.search("parameter\s*=\s*(.*)",line).groups()
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |