匹配所有内容的python列表
我可能没有正确询问:我想要一个可以匹配任何列表的列表值:(None,)
的“逆” 但即使使用 (None,)
,它也会将项目匹配为 None (我不想要)
重点是我有一个正在使用的函数: [x for x in my_list if x[ field] 不在 filter_list]
中
,我想过滤所有内容或不过滤任何内容,而不进行如下测试: if filter_list==(None,): return []
和 if filter_list==('*',): return my_list
PS:我想简化我的问题,导致一些错误(list
标识符)或愚蠢的事情[x for x in x]
;)
嗨,
我需要在python中使用列表理解进行一些过滤。
如果我这样做:
[x for x in list if x in (None,)]
我摆脱了所有的价值观,这很好,
但我希望有相同的东西来匹配
我可以做的一切:
[x for x in list if x not in (None,)]
但它不会与其余的同质
我尝试了一些东西但是例如 (True,)
仅匹配 1
请注意,要过滤的值是数字,但如果您有通用的内容(例如 (None,)
不匹配任何内容),那就太好了,
谢谢 路易斯
I probably didn't ask correctly: I would like a list value that can match any list: the "inverse" of (None,)
but even with (None,)
it will match item as None (which I don't want)
The point is I have a function working with: [x for x in my_list if x[field] not in filter_list]
and I would like to filter everything or nothing without making tests like:if filter_list==(None,): return []
and if filter_list==('*',): return my_list
PS: I wanted to simplify my question leading to some errors (list
identifier) or stupid thing [x for x in x]
;)
Hi,
I need to do some filtering with list comprehension in python.
if I do something like that:
[x for x in list if x in (None,)]
I get rid of all values, which is fine
but I would like to have the same thing to match everything
I can do something like:
[x for x in list if x not in (None,)]
but it won't be homogeneous with the rest
I tried some things but for example (True,)
matches only 1
Note than the values to filter are numeric, but if you have something generic (like (None,)
to match nothing), it would be great
Thanks
Louis
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
__contains__
是检查某些内容是否在序列中的神奇方法:__contains__
is the magic method that checks if something is in a sequence:更好的语法是:
The better syntax would be:
你是什么意思
Just do
并且
list
中的每个项目都匹配。What do you mean by
Just do
and every item in
list
is matched.您可以更改程序以接受过滤器对象,而不是列表。
抽象基本过滤器将有一个
matches
方法,如果 x *matches",则返回 true。您的一般情况过滤器将使用列表参数构建,并根据列表的成员资格进行过滤 -
matches
函数将搜索列表并返回true
如果参数在列表中,过滤器对象的两个特殊子类:none 和 all。
您还可以有 始终返回
true
(全部)或false
(无)的函数。You could change your program to accept a filter object, instead of a list.
The abstract base filter would have a
matches
method, that returns true if x *matches".Your general case filters would be constructed with a list argument, and would filter on membership of the list - the
matches
function would search the list and returntrue
if the argument was in the list.You could also have two special subclasses of the filter object : none and all.
These would have special match functions which either always return
true
(all) orfalse
(none).你不需要
if
,你可以直接说You don't need an
if
, you can just say要匹配所有内容,您不需要 if 语句
或如果您真的喜欢这样做
To match everything, you don't need if statement
or If you really like to do
回答您修改后的问题:“匹配”所有可能值的列表实际上是无限长度的。所以如果没有 if 测试,你就无法做你想做的事。我建议您的 arg 应该是一个列表或代表“全部”和“无”情况的两个值之一:
如果 filter_list 很大,您可能希望将最后一行替换为:
或者,不要打扰;只需记录filter_list(重命名为filter_collection)可以是任何支持
__contains__()
的内容,并提醒读者集合比列表更快。Answering your revised question: the list that "matches" all possible values is effectively of infinite length. So you can't do what you want to do without an if test. I suggest that your arg should be either a list or one of two values representing the "all" and "none" cases:
If filter_list is large, you may wish the replace the last line by:
Alternatively, don't bother; just document that filter_list (renamed as filter_collection) can be anything that supports
__contains__()
and remind readers that sets will be faster than lists.