zoukankan      html  css  js  c++  java
  • python函数-filter()

    filter(func, seq) 

    filter()函数是 Python 内置的另一个有用的高阶函数,filter()函数接收一个函数 f 和一个list,这个函数 f 的作用是对每个元素进行判断,返回 True或 False,filter()根据判断结果自动过滤掉不符合条件的元素,返回由符合条件元素组成的新list。

    实例:

    1 nums = [2,3,6,12,15,18]
    2 def nums_res (x):
    3   return x % 2 == 0 and x % 3 == 0
    4 print filter(nums_res, nums)
    5 执行结果:
    6 [6, 12, 18]

    等价于:

    1 >>> print [x for x in nums if x % 2 == 0 and x % 3 == 0]
    2 [6, 12, 18]
  • 相关阅读:
    win_tc使用感受
    10进制转8进制(栈操作)
    动态栈
    数组
    单链表学习
    static用法
    基础2
    linux c first
    linux net command /uboot command
    opencv
  • 原文地址:https://www.cnblogs.com/guyuyuan/p/6883721.html
Copyright © 2011-2022 走看看