"filtered view" upon lists?
Wildemar Wildenburger
wildemar at freakmail.de
Tue Sep 12 11:38:25 EDT 2006
Jorge Godoy wrote:
> Wildemar Wildenburger <wildemar at freakmail.de> writes:
>
>> I don't know how else to call what I'm currently implementing: An
object that
>> behaves like a list but doesn't store it's own items but rather
pulls them
>> from a larger list (if they match a certain criterion).
>> Changes to the filter are instantly reflected in the underlying list.
>> Clear enough?
>
> It looks like you're implementing a callable to me. This is a method
that
> returns results based on some input -- here your original list and
filter.
> Then you'll use this method wherever you need that filtered list.
>
Ok, so I'm not clear enough ;) .
I don't just want to extract certain elements from a list, I want an
object that looks like a list, however all changes made to that object
are automagically reflected in the original list. (I guess that is one
of those 'if it's hard to explain, ...' cases.)
I should have included an example right away ... here goes:
# I have a list
l = [1, 2, 3, 4, 5, 6, 7]
# I then want to create a Filter instance
# (Filter beeing a *class* implemented by me)
# where isEven() returns True whenever an item of l
# should be included in f (in this case, even numbers).
# (I'm asking if something like this exists in the libs
# or elsewhere)
f = Filter(l, isEven)
# The desired behavior now goes something like this:
f
>>> [2, 4, 6]
del f[1]
f
>>> [2, 6]
l
>>> [1, 2, 3, 5, 6, 7]
f.append(77)
f
>>> [2, 6, 77]
# 77 being intentionally uneven
l
>>> [1, 2, 3, 5, 6, 7, 77]
# could be [1, 2, 3, 5, 6, 77, 7] as well
# I don't care here
# and so forth ...
I think SQL views are the direct analog.
> I don't believe it is generic. Nobody knows your data specs or filtering
> needs.
Hence the additional function in the Filter constructor ;) . You suggest
the same thing below, so that is obviously no problem.
> Use of list comprehension might make it easier to code this:
>
> <snip elaborate example>
I sort of wanted to avoid these. Though my lists shouldn't terribly
long, so performance is not an issue so much. I simply want to avoid
having two datasets that I have to sync. Major pain, I believe.
Coding all that really is quite straight forward, but it is a lot of
mule-work. I hoped (no, I still hope) that there might be somethin like
that around already.
A bit clearer now?
bye
wildemar
More information about the Python-list
mailing list