Python:关于filter函数的一个问题

想问一下前四行代码和后三行有什么区别吗?为什么前面的那个可以后面的就不行呢?还有最后一排输出的那段蓝字是什么意思啊

img


谢谢!

[ item for item in filter(lambda x:x>5,a1) ]
等价于
list(filter(lambda x:x>5,a))
有没有转成list就是区别
把filter改成range也是差不多的结果

filter返回结果是个迭代器
想和前面一样需要使用list 转化一下

result = list(b)
print(result)


a1 = [1, 2, 3, 4, 5, 6, 7, 8]
# 这个是用列表生成式,循环了filter对象
a2 = [ item for item in filter(lambda x:x>5,a1) ]
print(a2)


a=(1, 2, 3, 4, 5, 6, 7, 8)
b = filter(lambda x:x>5,a)  #filter函数返回一个filter对象
#输出结果的时候,强制转换成了 list
print(list(b))

  • 你可以参考下这个问题的回答, 看看是否对你有帮助, 链接: https://ask.csdn.net/questions/7481756
  • 你也可以参考下这篇文章:Python中的filter()函数!!!1
  • 除此之外, 这篇博客: 如何使用Python的filter函数中的 用于复杂场景 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • 对于复杂的数据结构,filter()也可以胜任,例如,有一个由字典组成的列表,我们不仅要遍历列表中的每项(字典), 还可能要遍历字典中的每个键值对,以便得到所有的数据。

    举个例子,假设我们有水族馆里每种生物的一个列表以及每种生物的不同细节,用下面的列表显示此数据。

    aquarium_creatures = [
      {"name": "sammy", "species": "shark", "tank number": "11", "type": "fish"},
      {"name": "ashley", "species": "crab", "tank number": "25", "type": "shellfish"},
      {"name": "jo", "species": "guppy", "tank number": "18", "type": "fish"},
      {"name": "jackie", "species": "lobster", "tank number": "21", "type": "shellfish"},
      {"name": "charlie", "species": "clownfish", "tank number": "12", "type": "fish"},
      {"name": "olly", "species": "green turtle", "tank number": "34", "type": "turtle"}
    ]
    

    下面就写一个函数,用这个函数来过滤这些数据。为了让filter()访问每个字典和字典中的每个元素,这需要构造一个嵌套函数,如下所示:

    def filter_set(aquarium_creatures, search_string):
        def iterator_func(x):
            for v in x.values():
                if search_string in v:
                    return True
            return False
        return filter(iterator_func, aquarium_creatures)
    

    定义filter_set()函数,以aquarium_creaturessearch_string作为参数。在filter_set()中,将内部函数iterator_func()作为filter()的参数。filter_set()函数将返回由filter()生成的迭代器。

    iterator_func()x作为参数,它代表列表中的一个项(即单个字典)。

    接下来,for循环访问字典中每个键值对,然后使用条件语句检查search_string是键值对中的值。

    iterator_func函数作为filter函数的参数对象,用它对迭代对象进行筛选。例如:用filter_set()搜索字符串:

    filtered_records = filter_set(aquarium_creatures, "2")
    

    一旦函数执行完毕,过滤器对象存储在filtered_records变量中,我们将其转换为一个列表并打印:

    print(list(filtered_records))
    

    输出内容:

    [{'name': 'ashley', 'species': 'crab', 'tank number': '25', 'type': 'shellfish'}, {'name': 'jackie', 'species': 'lobster', 'tank number': '21', 'type': 'shellfish'}, {'name': 'charlie', 'species': 'clownfish', 'tank number': '12', 'type': 'fish'}]
    

    刚才的示例中,我们用filter()实现了在字典组成的列表中过滤制定字符。

  • 以下回答由chatgpt基于相关博客总结生成:

    前四行代码和后三行的区别在于接收结果的方式不同。前面的代码使用了list()函数将filter函数的返回值转换为列表,而后面的代码没有进行转换,直接输出了filter函数的返回值,这里的返回值是一个迭代器对象。

    最后一排蓝字是在介绍filter函数中使用lambda函数的一种方法。lambda函数可以用简洁的方式定义一个函数,并在filter函数中使用该函数进行筛选。

    如果要给出具体的解决方案,可以根据参考资料中的实例进行示范。以下是使用filter函数过滤出列表中的所有奇数的具体代码:

    def is_odd(n):
        return n % 2 == 1
    
    tmplist = filter(is_odd, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
    newlist = list(tmplist)
    print(newlist)
    

    输出结果为 [1, 3, 5, 7, 9],表示过滤出了列表中的所有奇数。

    另外,如果你想要过滤出1~100中平方根是整数的数,可以参考以下代码:

    import math
    
    def is_sqr(x):
        return math.sqrt(x) % 1 == 0
    
    tmplist = filter(is_sqr, range(1, 101))
    newlist = list(tmplist)
    print(newlist)
    

    输出结果为 [1, 4, 9, 16, 25, 36, 49, 64, 81, 100],表示过滤出了1~100中平方根是整数的数。