Python 3过滤器 - 错误或功能? [英] Python 3 filter - Bug or Feature?
问题描述
以下是与Python 2.7的交互式会话:
<$ p
$ b Python 2.7.3(默认,2013年1月2日,16:53:07)
[gcc 4.7.2] on linux2
键入help,版权,信用或许可证以获取更多信息。
>>> import re
>>> KEY =REC_PAPER
>>> VALIDVALUES = filter(lambda x:re.search(r'^'+ KEY +'\ = ',x),[
...REC_METAL = | YES | NO |,
...REC_PAPER = | YES | NO |,
...REC_GLASS = | YES | NO |,
...REC_PLAST = | YES | NO |,
...DEBUG_FLAG = | 0 | 1 |
...])#结束一般清单。
>>> print(VALIDVALUES)
['REC_PAPER = | YES | NO |']
>>>
这是我期望VALIDVALUES返回。然而,Python 3.2的交互式会话产生了完全不同的结果:
Python 3.2.3(默认,2013年2月20日, 17:02:41)
[gcc 4.7.2] on linux2
输入help,copyright,credits或license以获取更多信息。
>>> import re
>>> KEY =REC_PAPER
>>> VALIDVALUES = filter(lambda x:re.search(r'^'+ KEY +'\ = ',x),[
...REC_METAL = | YES | NO |,
...REC_PAPER = | YES | NO |,
...REC_GLASS = | YES | NO |,
...REC_PLAST = | YES | NO |,
...DEBUG_FLAG = | 0 | 1 |
...])#结束一般清单。
>>> print(VALIDVALUES)
0xb734268c处的<filter对象>
>>>
我曾经在几个地方看过(包括stackoverflow),Python对Perl的grep和列表的等价物是过滤列表。这似乎在Python 2中工作。但是,假设在Python 3中的上述行为是正确的,似乎不再是这种情况。
第一个问题:是第二个问题:假设它是一个特性,我如何得到Python 2给出的输出结果?由于我不想进入的原因,我想远离定义一个函数或子程序,并像目前的代码一样内联。
我是否缺少显而易见的东西(对于新手来说很可能)?提前致谢。
在Python 3.x中使用functions.html#filterrel =nofollow>文档, filter
返回一个迭代器比 2.x版中的列表要多。这比预先生成整个列表更有记忆效率。如果你想返回列表,你可以将迭代器包装在列表()
调用中: VALIDVALUES = list(filter(...))
Okay, I am a complete newbie to Python - and stackoverflow. I am coming from a ksh and Perl background.
The following in an interactive session with Python 2.7:
Python 2.7.3 (default, Jan 2 2013, 16:53:07) [GCC 4.7.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import re >>> KEY="REC_PAPER" >>> VALIDVALUES=filter(lambda x:re.search(r'^' + KEY + '\=', x), [ ... "REC_METAL=|YES|NO|", ... "REC_PAPER=|YES|NO|", ... "REC_GLASS=|YES|NO|", ... "REC_PLAST=|YES|NO|", ... "DEBUG_FLAG=|0|1|" ... ]) #End general list. >>> print(VALIDVALUES) ['REC_PAPER=|YES|NO|'] >>>
Which is what I would expect VALIDVALUES to return. However, Python 3.2's interactive session yields completely different results:
Python 3.2.3 (default, Feb 20 2013, 17:02:41) [GCC 4.7.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import re >>> KEY="REC_PAPER" >>> VALIDVALUES=filter(lambda x:re.search(r'^' + KEY + '\=', x), [ ... "REC_METAL=|YES|NO|", ... "REC_PAPER=|YES|NO|", ... "REC_GLASS=|YES|NO|", ... "REC_PLAST=|YES|NO|", ... "DEBUG_FLAG=|0|1|" ... ]) #End general list. >>> print(VALIDVALUES) <filter object at 0xb734268c> >>>
I have seen in several places (including stackoverflow) where Python's equivalent of Perl's grep against a list is to filter the list. That appeared to work in Python 2. However, assuming the above behaviour in Python 3 is "correct," that no longer seems to be the case.
First question: Is the above beahviour a bug or feature in Python 3?
Second question: Assuming it is a feature, how do I get the output that Python 2 was giving? For reasons I won't go into, I want to stay away from defining a function or subroutine, and do it "inline" like the current code.
Am I missing something obvious (quite possible for a newbie)? Thanks in advance.
As per the documentation, filter
in Python 3.x returns an iterator, rather than a list as in version 2.x. This is more memory-efficient than generating the whole list up-front. If you want the list back, you can wrap the iterator in a list()
call:
VALIDVALUES = list(filter(...))
这篇关于Python 3过滤器 - 错误或功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!