473,473 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Mapping and Filtering Help for Lists


Alright I got asked today by a friend this question, which obviously I
couldn't help him with.

He needs to get rid of words in a string referring to an already given list
then needs to map them using a function he already has. Ill explain this
better by giving an example :P

Say ur given these lists:

un_words = ['a', 'the', 'he', 'she', 'uses', 'with']
alterns = [ ['book', 'textbook', 'notepad'], ['pencil', 'pen', 'pacer'] ]

The problem asks to create a "compareandremove" so that you can use it on a
string, to remove the words from the string that are contained in un_words.

The remaining words then need to be compared to the alterns list and either
bring back the word if no matches or bring back the list. To better explain
that i'll use an example.

If i do compareandremove('notepad a pencil with desk')

I need it so it removes words contained in un_words, so "a" and "with";
then compares the remaining words to alterns to find a match.

This should bring back:

['notepad', 'book', 'textbook', 'pencil', 'pen', 'pacer', 'desk']
Any tips on how to create this function or maybe the function itself so I
can then show him how to do it.

Thank you.
--
View this message in context: http://www.nabble.com/Mapping-and-Fi...p16925836.html
Sent from the Python - python-list mailing list archive at Nabble.com.

Jun 27 '08 #1
3 1364
Zethex <ze****@hotmail.comwrites:
Alright I got asked today by a friend this question, which obviously I
couldn't help him with.

He needs to get rid of words in a string referring to an already given list
then needs to map them using a function he already has. Ill explain this
better by giving an example :P

Say ur given these lists:

un_words = ['a', 'the', 'he', 'she', 'uses', 'with']
alterns = [ ['book', 'textbook', 'notepad'], ['pencil', 'pen', 'pacer'] ]

The problem asks to create a "compareandremove" so that you can use it on a
string, to remove the words from the string that are contained in un_words.

The remaining words then need to be compared to the alterns list and either
bring back the word if no matches or bring back the list. To better explain
that i'll use an example.

If i do compareandremove('notepad a pencil with desk')

I need it so it removes words contained in un_words, so "a" and "with";
then compares the remaining words to alterns to find a match.

This should bring back:

['notepad', 'book', 'textbook', 'pencil', 'pen', 'pacer', 'desk']
Any tips on how to create this function or maybe the function itself so I
can then show him how to do it.

Thank you.
Look away now if you don't want a complete solution!

un_words = ['a', 'the', 'he', 'she', 'uses', 'with']
alterns = [ ['book', 'textbook', 'notepad'], ['pencil', 'pen', 'pacer'] ]

# these words will be replaced with nothing
wordmap = dict((w, []) for w in un_words)

# these words will be replaced with the list of their synonyms
for wordlist in alterns:
for word in wordlist:
wordmap[word] = wordlist

def compareandremove(sentence):
return [x for w in sentence.split() for x in wordmap.get(w, [w])]
# Example
>>compareandremove('notepad a pencil with desk')
['book', 'textbook', 'notepad', 'pencil', 'pen', 'pacer', 'desk']

--
Arnaud
Jun 27 '08 #2

Thank you a lot!

Another quick couple of questions.

How can i make it so it removes special operators such as ? ! or doesnt
include them?

and

I need to lowercase the words so should i use sentence = sentence.lower()?
--
View this message in context: http://www.nabble.com/Mapping-and-Fi...p16926760.html
Sent from the Python - python-list mailing list archive at Nabble.com.

Jun 27 '08 #3
Zethex <ze****@hotmail.comwrites:
Thank you a lot!

Another quick couple of questions.

How can i make it so it removes special operators such as ? ! or doesnt
include them?
As you are doing lots of string manipulations, I advise you to read
the section of the python documentation on strings (link below). This
way you will get a good idea of what can be done out of the box with
strings in python.

http://docs.python.org/lib/string-methods.html

(Or type help(str) from the interactive prompt)

For the particular task of removing a character, you may be interested
in the replace() method.
and

I need to lowercase the words so should i use sentence = sentence.lower()?
Yes.

Or you could combine the two by using the translate() method. E.g.
>>table = ''.join(chr(c).lower() for c in range(256))
'You! are Free??'.translate(table, '!?')
'you are free'

HTH

--
Arnaud
Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: kevinold | last post by:
Hello everyone, I have a list of about 1600 employees that I'd like to have displayed in a form. I'd like to make the "search" for the user as easy as possible. I ran across this:...
2
by: Vic | last post by:
Dear All, I am getting the following error message : "You cannot assign a value to this object" ("Me.filter =" is highlighted) I have two comboboxes (ByGenes and BySpecies)with lists in them...
5
by: sensible | last post by:
Can I solve this problem using Access? If so, will some give this newbie a simple step by step on how to go about it, please....all the way from File/New. In Excel I am using...
0
by: Scott Loupin | last post by:
I've got two databases with similar data in them (WestSide and EastSide). I've set up two identical reports that is filtered by date and the client name. I'm using one form to do the filtering. ...
0
by: Patrick | last post by:
I'm working on a contact management application, and need a hand with one aspect... Here's what I want to create: ------------------------------------ A form split into two parts. There is a...
1
by: rk | last post by:
Hi, I need to provide filtering feature for the columns in datagrid. for that I need to provide a drop down arrow image/button in some columns and when that arrow button is clicked, it should...
0
by: findus2007 | last post by:
Hi all, I have got an XML file, which I produced by serializing an object, which represents my data I want to be able to work with in Excel (Office 2003, Interop, no VSTO). When I do the mapping...
10
by: Redbeard | last post by:
Hi, I am a newbie using Access 2003. I am trying to apply a filter to a form and then re-filtering that forms records again. Basically I have my main form and when I wish to filter I click a button...
0
by: Zethex | last post by:
Thank you for the help so far. Another quick question, how can I remove the special characters such as ! and ?. I also need to lowercase the words so should i use sentence = sentence.lower()...
0
Oralloy
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
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...
0
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.