回数是指从左向右读和从右向左读都是一样的数,例如12321,909。请利用filter()筛选出回数

链接源:http://taohuayuan.fun/post/2/

#
coding=utf-8 #计算数值的位数 def places(n): i=0 while n>1: n=n/10 i=i+1 return i #测试1 print(places(1234546)) #is_palindrome函数返回bool值,是否为回数 def is_palindrome(n): #将数值的每一位保存在列表中, L = [] num=places(n) for i in range(1, num+ 1): L.append(n % 10) n = int(n / 10) #cs表示数值中相等数字的个数,如12321,相等数字的个数为2 cs=0 for i in range(int(num/2)): if L[i]==L[-1-i]: cs=cs+1 else: cs=cs+0 return cs==int(num/2) #测试2 print(is_palindrome(12321)) print(is_palindrome(567834)) #测试3 print(list(filter(is_palindrome,[12312,12321,5678765,218,472839420])))

 

posted @ 2019-01-10 12:01  CN海盗船长  阅读(1037)  评论(0编辑  收藏  举报