“过滤后的视图”列表? [英] "filtered view" upon lists?

查看:56
本文介绍了“过滤后的视图”列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨那里:)


我不知道怎么称呼我正在实施的目标:一个对象

表现得像一个列表,但不存储它自己的项目,而是

从更大的列表中提取它们(如果它们符合某个标准)。

更改为过滤器会立即反映在基础列表中。

足够清楚?


好​​的,所以我认为这是通用的,足以在某些

标准模块已经存在(我已经经常这么做了:痛苦地实现了s / th
,然后在几周后在libs中找到它。)。


任何指针?


cu

wildemar

解决方案

< blockquote> Wildemar Wildenburger< wi ****** @freakmail.dewrites:


我不知道怎么称呼我当前实现:

的行为就像一个列表,但不是'不要存储它自己的物品,而是从更大的列表中拉出它们(如果它们符合某个标准)。

对过滤器的更改会立即反映在基础清单。

足够清楚?



看起来你正在为我实现一个可调用的。这是一种方法,

根据一些输入返回结果 - 这里是你的原始列表和过滤器。


然后你会在任何地方使用这种方法需要那个过滤后的列表。


好​​的,所以我认为这是通用的,足以在一些标准的

模块中找到(我'经常这样做:痛苦地实施s / th和

然后在几周后在libs中找到它。)。



我不相信它是通用的。没人知道你的数据规格或过滤

需要。


任何指针?



使用list comprehension可以更容易地对此进行编码:

def myCallable(my_list,filter):

filtered_list = [(item)my_list中的项目,如果过滤(项目)]

返回filtered_list

完整代码示例:


>> test_list = range(10)
filter = lambda x:not x%2
def myCallable(list,filter):



.... filtered_list = [(item)for item in item if if filter(item) )]

....返回filtered_list

....


>> myCallable(test_list,filter)



[0,2,4 ,6,8]


>>用于myCallable中的项目(test_list,过滤器):



.... print"看?我是&,'项目

....

见?我是0

见?我是2

见?我是4

见?我是6

见?我是8


>>>



-

Jorge Godoy< jg **** @ gmail.com>


" Jorge Godoy" < jg **** @ gmail.com写信息

news:87 ************ @ gmail.com ...


Wildemar Wildenburger< wi ****** @ freakmail.dewrites:


>我不知道如何或者打电话给我正在实施的目标:一个对象

表现得像一个列表,但不存储它自己的项目,而是拉动它们
从更大的列表(如果它们符合某个标准)。
过滤器的更改会立即反映在基础列表中。
足够清楚?



看起来你正在为我实现一个可调用的。这是一种方法,

根据一些输入返回结果 - 这里是你的原始列表和过滤器。


然后你会在任何地方使用这种方法需要过滤列表。


>好的,所以我认为这已经足够通用了,可以在某些标准的
模块中找到(我已经经常这样做:痛苦地实施s / th
然后几周后在libs中找到它。)。



我不相信它是通用的。没有人知道您的数据规格或过滤

需要。


>任何指针?



使用list comprehension可能会更容易编码:


def myCallable(my_list,filter):

filtered_list = [(item)my_list中的项目,如果过滤(项目)]

返回filtered_list


完整代码示例:< br>


>>> test_list = range(10)
filter = lambda x:not x%2
def myCallable(list,filter):



... filtered_list = [(item)for list in item if if filter(item)]

...返回filtered_list

...


>> ;> myCallable(test_list,filter)



[0,2,4,6,8]


>>>用于myCallable中的项目(test_list,过滤器):



...打印看?我是&,'项目

...

见?我是0

见?我是2

见?我是4

见?我是6

见?我是8


>>>>




-

Jorge Godoy< jg **** @ gmail.com>



从功能上讲,还有内置的过滤器可以做同样的事情:



>> data = range(10)
odds = filter(lambda x:x%2,data)
evens = filter(lambda x:x%2!= 1,数据)
赔率



[ 1,3,5,7,9]


>> evens



[0,2,4,6,8]

- Paul


Jorge Godoy写道:


Wildemar Wildenburger< wi ****** @ freakmail.dewrites:


>我不知道如何调用我正在实现的内容:



对象那个


>表现得像一个列表,但不存储它自己的项目,而是



从更大的列表中提取


>(如果匹配某个标准)。
对过滤器的更改会立即反映在基础列表中。
足够清楚?



看起来你正在为我实现一个可调用的。这是一个方法





根据一些输入返回结果 - 这里是原始列表和



过滤器。


然后你将在需要过滤列表的地方使用此方法。



好​​的,所以我不够清楚;)。

我不只是想从列表中提取某些元素,我想要一个看起来像列表的

对象但是,对该对象所做的所有更改

都会自动反映在原始列表中。 (我想这是一个

的那些'',如果它很难解释,......''的情况。)


我应该有马上包括一个例子......这里是:


#我有一个清单

l = [1,2,3,4,5,6, 7]


#然后我要创建一个Filter实例

#(过滤蜜蜂* *由我实现)

#其中isEven()返回True,只要在f中包含l

#的项目(在这种情况下,偶数)。

#(我是询问libs中是否存在这样的东西

#或其他地方

f =过滤器(l,isEven)


#期望的行为现在是这样的:

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故意不平衡

l


>> [1, 2,3,5,6,7,87



#可能是[1,2,3,5,6, 77,7]以及

#我不在乎这个


#等等......


我认为SQL视图是直接模拟的。


我不相信它是通用的。没人知道您的数据规格或过滤

需求。



因此,Filter构造函数中的附加函数;)。你建议

下面同样的事情,所以这显然没问题。


使用list comprehension可能会更容易编码:


< snip详细示例>



我有点想避免这些。虽然我的名单不应该太长,但是表现并不是一个问题。我只想避免

有两个我必须同步的数据集。我相信主要的痛苦。

编码所有这些都非常直接,但是很多

骡子工作。我希望(不,我仍然希望)可能会有类似

的东西。


现在有点清楚了吗?


再见

wildemar


Hi there :)

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?

Ok, so I figured that this is generic enough to be found in some
standard module already (I''ve had this often enough: Painfully
implementing s/th and then finding it in the libs some weeks later.).

Any pointers?

c.u.
wildemar

解决方案

Wildemar Wildenburger <wi******@freakmail.dewrites:

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 figured that this is generic enough to be found in some standard
module already (I''ve had this often enough: Painfully implementing s/th and
then finding it in the libs some weeks later.).

I don''t believe it is generic. Nobody knows your data specs or filtering
needs.

Any pointers?

Use of list comprehension might make it easier to code this:
def myCallable(my_list, filter):
filtered_list = [(item) for item in my_list if filter(item)]
return filtered_list
Example of full code:

>>test_list = range(10)
filter = lambda x: not x%2
def myCallable(list, filter):

.... filtered_list = [(item) for item in list if filter(item)]
.... return filtered_list
....

>>myCallable(test_list, filter)

[0, 2, 4, 6, 8]

>>for item in myCallable(test_list, filter):

.... print "See? I''m", item
....
See? I''m 0
See? I''m 2
See? I''m 4
See? I''m 6
See? I''m 8

>>>


--
Jorge Godoy <jg****@gmail.com>


"Jorge Godoy" <jg****@gmail.comwrote in message
news:87************@gmail.com...

Wildemar Wildenburger <wi******@freakmail.dewrites:

>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 figured that this is generic enough to be found in some standard
module already (I''ve had this often enough: Painfully implementing s/th
and
then finding it in the libs some weeks later.).


I don''t believe it is generic. Nobody knows your data specs or filtering
needs.

>Any pointers?


Use of list comprehension might make it easier to code this:
def myCallable(my_list, filter):
filtered_list = [(item) for item in my_list if filter(item)]
return filtered_list
Example of full code:

>>>test_list = range(10)
filter = lambda x: not x%2
def myCallable(list, filter):

... filtered_list = [(item) for item in list if filter(item)]
... return filtered_list
...

>>>myCallable(test_list, filter)

[0, 2, 4, 6, 8]

>>>for item in myCallable(test_list, filter):

... print "See? I''m", item
...
See? I''m 0
See? I''m 2
See? I''m 4
See? I''m 6
See? I''m 8

>>>>



--
Jorge Godoy <jg****@gmail.com>

Functionally-speaking, there is the filter built-in that does this same
thing:

>>data = range(10)
odds = filter(lambda x : x % 2, data)
evens = filter(lambda x : x % 2 != 1, data)
odds

[1, 3, 5, 7, 9]

>>evens

[0, 2, 4, 6, 8]
-- Paul


Jorge Godoy wrote:

Wildemar Wildenburger <wi******@freakmail.dewrites:

>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


这篇关于“过滤后的视图”列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆