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'))
>
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 b
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
>
>
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_d
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:
> # d
> 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' inst
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 m
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"
# case-ins
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 ot
> 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 n
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 solu
> 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 co
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.
--
http://mail.python.org/mailman/listinfo/python-list
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'
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']
>
> 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 m
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 eleme
> 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 usuall
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?
19 matches
Mail list logo