Re: Mapping and Filtering Help for Lists

2008-04-27 Thread Eric Wertman
You should check out the sets module:

http://docs.python.org/lib/module-sets.html




  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']
--
http://mail.python.org/mailman/listinfo/python-list


Re: Mapping and Filtering Help for Lists

2008-04-27 Thread Arnaud Delobelle
Zethex [EMAIL PROTECTED] writes:

 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
--
http://mail.python.org/mailman/listinfo/python-list


Re: Mapping and Filtering Help for Lists

2008-04-27 Thread Zethex

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
-- 
View this message in context: 
http://www.nabble.com/Mapping-and-Filtering-Help-for-Lists-tp16925836p16926758.html
Sent from the Python - python-list mailing list archive at Nabble.com.

--
http://mail.python.org/mailman/listinfo/python-list


Re: Mapping and Filtering Help for Lists

2008-04-27 Thread Zethex

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-Filtering-Help-for-Lists-tp16925836p16926760.html
Sent from the Python - python-list mailing list archive at Nabble.com.

--
http://mail.python.org/mailman/listinfo/python-list


Re: Mapping and Filtering Help for Lists

2008-04-27 Thread Arnaud Delobelle
Zethex [EMAIL PROTECTED] writes:

 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
--
http://mail.python.org/mailman/listinfo/python-list