zoukankan      html  css  js  c++  java
  • Python 内建的filter()函数用于过滤序列。

    例如,在一个list中,删掉偶数,只保留奇数,可以这么写:

    def is_odd(n):
        return n % 2 == 1
    
    list(filter(is_odd, [1, 2, 4, 5, 6, 9, 10, 15]))
    # 结果: [1, 5, 9, 15](返回False就过滤掉)

    习题:回数是指从左向右读和从右向左读都是一样的数,例如12321909。请利用filter()滤掉非回数:

    # -*- coding: utf-8 -*-
    from functools import reduce

    def is_palindrome(n):
      lenN=len(str(n))
      #print(lenN)
      n=str(n)
      #print(n)
      if lenN<2: return False
      for i in range(lenN//2):
        j=i+1
        if n[i]!=n[-j]:
          return False
      return True

    output = filter(is_palindrome, range(1, 100000))
    print(list(output))

     
  • 相关阅读:
    优化-IO
    优化-cpu
    优化-内存
    系统优化
    snort -- 入侵检测系统
    tripwire--入侵检测系统
    sudo
    selinux
    pptpd
    C++ 内联函数
  • 原文地址:https://www.cnblogs.com/alamZ/p/6549547.html
Copyright © 2011-2022 走看看