Discuss / Python / 再简单的代码也要亲手敲一遍

再简单的代码也要亲手敲一遍

Topic source

翁岚敏

#1 Created at ... [Delete] [Delete and Lock User]

do_filter.py

!/usr/bin/env python3

-- coding:utf-8 --

def is_odd(n): return n%2 ==1

L= range(100)

print(list(filter(is_odd,L)))

def not_empty(s): return s and s.strip()

print(list(filter(not_empty,['A','','B',None,'C',' '])))

prime_number.py

def main(): for n in primes(): if n < 1000: print(n) else: break

def _odd_iter(): n = 1 while True: n = n + 2 yield n

def _not_divisible(n): return lambda x: x% n>0

def primes(): yield 2 it = _odd_iter() while True: n = next(it) yield n it = filter(_not_divisible(n), it)

if name== 'main': main()


  • 1

Reply