Discuss / Python / 作业

作业

Topic source

用生成器的版本: def all_shuzi(): n = 1 while True: yield n n += 1

def is_palindrome(n): return n == int(str(n)[::-1])

it = filter(is_palindrome,all_shuzi())

for i in it: if i < 1000: print(i) else: break

一行代码版: print(list(filter(lambda n:n == int(str(n)[::-1]),list(range(1000)))))

主要是用切片功能就很方便,这也是Python特有的。


  • 1

Reply