Python 3 filter - Bug or Feature?

前端 未结 2 1497
梦毁少年i
梦毁少年i 2021-01-27 08:41

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:

2条回答
  •  终归单人心
    2021-01-27 09:33

    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(...))
    

    Alternatively, and as recommended by What’s New In Python 3.0, you could rewrite it as a list comprehension without a lambda:

    VALIDVALUES = [x for x in [...] if re.search(r'^' + KEY + '\=', x)]
    

提交回复
热议问题