Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Python question

Name: Anonymous 2011-12-20 5:33

How can I remove all the words in one list from another list without loosing duplicates or order?

eg:

aList = ['so', 'what', 'man', 'what']

noiseWords = ['so', 'um', 'crap']

I want aList without the 'so' in noiseWords.
Using Python 3.1 btw, thanks for any help.

Name: Anonymous 2011-12-20 5:45

OP here,
One way I know how to do it is below, but duplicates get removed which I do not want.

code:
--------------------------------------------

aList = ['so', 'what', 'man', 'what']
noiseWords = ['so', 'um', 'crap']

newList = (list(set(aList) - set(noiseWords)))
print (newList)

--------------------------------------------

result:  ['what', 'man']

result I'm after:   ['what', 'man', 'what']

Name: Anonymous 2011-12-20 5:52

the easiest way would probably be to convert the noiseWords to a hash set of words, so that look ups can be done in constant time. Then you can step through aList, checking to see if each string in aList appears in noiseWords via a hash table look up. This will maintain the ordering in aList, and duplicate strings in aList that do not appear in noiseWords will still remain.

Name: Anonymous 2011-12-20 6:06

>>3
Ok thanks, I'll look into that.

Name: Anonymous 2011-12-20 6:43

[a for a in aList if a not in noiseWords]

Name: Anonymous 2011-12-20 6:44

>>4
LOOK INTO MY ANUS

Name: Anonymous 2011-12-20 7:56

what if noisewors contains a word twice?

if that never happens, i'd go with something like:


done = []
[a for a in aList if a not in noiseWords and ++(done[a]) == 1]

Name: Anonymous 2011-12-20 8:36

>>7 `
Python
Expecting something as intuitive as ++(done[a]) to work.

Prepare to be disciplined by guido.

(Also, your solution sucks and does not make sense.)

Name: Anonymous 2011-12-20 8:48


 > (remove* '("so" "um" "crap") '("so" "what" "man" "what"))
 '("what" "man" "what")

Name: Anonymous 2011-12-20 22:32

>>9
LISP is shit!

Don't change these.
Name: Email:
Entire Thread Thread List