remove a list from a list

2006-11-17 Thread Rares Vernica
Hi, I have the following problem: I have a list like e = ['a', 'b', 'e'] and another list like l = ['A', 'a', 'c', 'D', 'E'] I would like to remove from l all the elements that appear in e case-insensitive. That is, the result would be r = ['c', 'D'] What is a *nice* way of doing it?

Re: remove a list from a list

2006-11-17 Thread Tim Chase
I have a list like e = ['a', 'b', 'e'] and another list like l = ['A', 'a', 'c', 'D', 'E'] I would like to remove from l all the elements that appear in e case-insensitive. That is, the result would be r = ['c', 'D'] What is a *nice* way of doing it? Well, it's usually

Re: remove a list from a list

2006-11-17 Thread Rares Vernica
That is a nice solution. But, how about modifying the list in place? That is, l would become ['c', 'D']. Thanks a lot, Ray Tim Chase wrote: I have a list like e = ['a', 'b', 'e'] and another list like l = ['A', 'a', 'c', 'D', 'E'] I would like to remove from l all the elements that

Re: remove a list from a list

2006-11-17 Thread Tim Chase
That is a nice solution. But, how about modifying the list in place? That is, l would become ['c', 'D']. e = ['a', 'b', 'e'] l = ['A', 'a', 'c', 'D', 'E'] s = set(e) [x for x in l if x.lower() not in s] ['c', 'D'] Well...changing the requirements midstream, eh? ;-) You can

Re: remove a list from a list

2006-11-17 Thread Fredrik Lundh
Rares Vernica wrote: I have the following problem: I have a list like e = ['a', 'b', 'e'] and another list like l = ['A', 'a', 'c', 'D', 'E'] I would like to remove from l all the elements that appear in e case-insensitive. That is, the result would be r = ['c', 'D'] What

Re: remove a list from a list

2006-11-17 Thread Rares Vernica
Yeah, I ended up doing a similar kind of loop. That is pretty messy. Is there any other way? Thanks, Ray Tim Chase wrote: That is a nice solution. But, how about modifying the list in place? That is, l would become ['c', 'D']. e = ['a', 'b', 'e'] l = ['A', 'a', 'c', 'D', 'E'] s =

Re: remove a list from a list

2006-11-17 Thread Fredrik Lundh
Fredrik Lundh wrote: What is a *nice* way of doing it? r = [i for i in e if i not in l] and swap l and e, add a few calls to lower, and start using better variable names in the future. /F -- http://mail.python.org/mailman/listinfo/python-list

Re: remove a list from a list

2006-11-17 Thread Tim Chase
Yeah, I ended up doing a similar kind of loop. That is pretty messy. Is there any other way? I've already provided 2 (or 3 depending on how one counts) solutions, each of which solve an interpretation of your original problem, neither of which involve more than 3 lines of fairly clean

Re: remove a list from a list

2006-11-17 Thread John Henry
from sets import Set as set # Python 2.3 b = list( set([i.upper() for i in b) - set([i.upper() for i in a] ) ) Rares Vernica wrote: Yeah, I ended up doing a similar kind of loop. That is pretty messy. Is there any other way? Thanks, Ray Tim Chase wrote: That is a nice solution.

Re: remove a list from a list

2006-11-17 Thread Tim Chase
from sets import Set as set # Python 2.3 b = list( set([i.upper() for i in b) - set([i.upper() for i in a] ) ) Just a caveat...this can change the order of items in the results as sets (and their differences) are inherently unordered data structures. If order of the items in the list

Re: remove a list from a list

2006-11-17 Thread John Henry
Scratch that. b becomes all upper... John Henry wrote: from sets import Set as set # Python 2.3 b = list( set([i.upper() for i in b) - set([i.upper() for i in a] ) ) Rares Vernica wrote: Yeah, I ended up doing a similar kind of loop. That is pretty messy. Is there any other way?

Re: remove a list from a list

2006-11-17 Thread Rares Vernica
Sorry for not being clear from the beginning and for not using clear variable names. Problem context: import os dirs_exclude = set(('a', 'b', 'e')) for root, dirs, files in os.walk('python/Lib/email'): # Task: # delete from dirs the directory names from dirs_exclude #

Re: remove a list from a list

2006-11-17 Thread John Henry
The curve ball is the case insensitivity otherwise it's a straightforward set operation. I wonder if it's possible to sub-class set and make the item comparision case insensitive. Anybody knows how to do that? Tim Chase wrote: Yeah, I ended up doing a similar kind of loop. That is pretty

Re: remove a list from a list

2006-11-17 Thread Matimus
The solution so far is: for i in xrange(len(dirs), 0, -1): if dirs[i-1].lower() in dirs_exclude: del dirs[i-1] This won't affect much, but uses better style I think. Change: for i in xrange(len(dirs),0,-1): To: for i in reversed(xrange(len(dirs))): and then just use 'i' instead

Re: remove a list from a list

2006-11-17 Thread Neil Cerutti
On 2006-11-17, Rares Vernica [EMAIL PROTECTED] wrote: Sorry for not being clear from the beginning and for not using clear variable names. Problem context: import os dirs_exclude = set(('a', 'b', 'e')) for root, dirs, files in os.walk('python/Lib/email'): # Task: # delete from

Re: remove a list from a list

2006-11-17 Thread John Henry
OK, if you don't care the resulting order, do it like: class Convert2Dict: def __init__(self, data): self._data={} for x in data: self._data[x.upper()]=x def get(self, key): return self._data[key.upper()] a = [a, B] b = [c, a, A, D, b] b_dict =

Re: remove a list from a list

2006-11-17 Thread Steven D'Aprano
On Fri, 17 Nov 2006 12:00:46 -0800, Rares Vernica wrote: Problem context: import os dirs_exclude = set(('a', 'b', 'e')) for root, dirs, files in os.walk('python/Lib/email'): # Task: # delete from dirs the directory names from dirs_exclude # case-insensitive The solution

Re: remove a list from a list

2006-11-17 Thread Rares Vernica
The problem with skipping over them is that walk would still walk them and their content. If they have a lot of other dirs and files inside then this might end up being time consuming. Thanks, Ray Neil Cerutti wrote: On 2006-11-17, Rares Vernica [EMAIL PROTECTED] wrote: Sorry for not being

Re: remove a list from a list

2006-11-17 Thread Rares Vernica
This solution I think is pretty nice: source[:] = [x for x in source if x.lower() not in target] Thanks a lot for all the answers, Ray Steven D'Aprano wrote: On Fri, 17 Nov 2006 12:00:46 -0800, Rares Vernica wrote: Problem context: import os dirs_exclude = set(('a', 'b', 'e')) for root,