Python question
1
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.
2
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']
3
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.
4
Name:
Anonymous
2011-12-20 6:06
>>3
Ok thanks, I'll look into that.
5
Name:
Anonymous
2011-12-20 6:43
[a for a in aList if a not in noiseWords]
6
Name:
Anonymous
2011-12-20 6:44
7
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]
8
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.)
9
Name:
Anonymous
2011-12-20 8:48
> (remove* '("so" "um" "crap") '("so" "what" "man" "what"))
'("what" "man" "what")
10
Name:
Anonymous
2011-12-20 22:32