1. 程式人生 > >Python學習筆記——filter

Python學習筆記——filter

回數是指從左向右讀和從右向左讀都是一樣的數,例如12321909。請利用filter()篩選出回數

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

output = filter(is_palindrome, range(1, 1000))
print('1~1000:', list(output))
if list(filter(is_palindrome, range(1, 200))) == [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 101, 111, 121, 131, 141, 151, 161, 171, 181, 191]:
    print('測試成功!')
else:
    print('測試失敗!')