473,729 Members | 2,331 Online
Bytes | Software Development & Data Engineering Community
+ 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 "compareandremo ve" 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 compareandremov e('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 1382
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 "compareandremo ve" 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 compareandremov e('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 compareandremov e(sentence):
return [x for w in sentence.split( ) for x in wordmap.get(w, [w])]
# Example
>>compareandrem ove('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??'.transla te(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
3885
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: http://www.barelyfitz.com/projects/filterlist/ filter that works quite well with small lists (on the site is a list of 20 names). It also works quite well in Firefox 1.5, but crawls in IE 6.
2
1592
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 on a form. I want to be able to do filtering on a form by connecting two criteria with a boolean expression (and). I want to get the two criteria selected from the comboboxes then the form should be filtered
5
328
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 data/filter/autofilter to filter my long list of words, one per row...trouble is in my Excel the columns are only 60,000 rows deep, and my word list is 1,500,000, This means I have to break my list into 26 seperate lists. This makes filtering very...
0
1738
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. Currently, I've used queries to combine the two client lists, so when I open the combo box, I have many names I need to scroll through until I find the one I want. How can I use filtering to only see clients from one database or the other,...
0
3644
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 datagrid on the left side that lists names and perhaps a couple of other key fields. The user can click on a record in the datagrid, which should automatically pull up details on that record in the various text boxes and other controls on the right...
1
1154
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 display the dropdownlist/listbox with the distinct values for that column so that user can filter the rows using those column values. can anybody tell me how I can get the dropdown to look like an arrow image first and then expand underneath when...
0
1759
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 by looping over different branches in my XML and calling: cell.XPath.SetValue(map, myXPath, myNamespace, false); everything works fine. But for large objects I noticed, that this mapping
10
1791
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 and a pop-up box comes up with several lists. I select what I want from the lists and click the button on the pop-up to perform the filter. It filters my main form, however if I want to filter again to narrow my search then I do the same thing and...
0
161
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() ? Thank you --
0
8761
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9426
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9281
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9200
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8148
agi2029
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6722
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
3238
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 we have to send another system
2
2680
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2163
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.