Using the filter function selected Slither

Using the filter function selected Slither

Title:
Slither refers to read from right to left and read from left to right are the same number, for example 12321,909
please use the filter () return the number of screened

from functools import reduce
def is_palindrome(n):
    l = list(str(n)) #['1','0' ]
    l.reverse() # ['0','1'] #
    # print(l.reverse())
    # 1*10+0 = 10
    num = reduce(lambda x,y:int(x) * 10 + int(y),l)
    return num == n
a= is_palindrome('10')
print(a)
output = filter(is_palindrome,range(1,1000))
print('1~1000:',list(output))

Output:

False
1~1000: [11, 22, 33, 44, 55, 66, 77, 88, 99, 101, 111, 121, 131, 141, 151, 161, 171, 181, 191, 202, 212, 222, 232, 242, 252, 262, 272, 282, 292, 303, 313, 323, 333, 343, 353, 363, 373, 383, 393, 404, 414, 424, 434, 444, 454, 464, 474, 484, 494, 505, 515, 525, 535, 545, 555, 565, 575, 585, 595, 606, 616, 626, 636, 646, 656, 666, 676, 686, 696, 707, 717, 727, 737, 747, 757, 767, 777, 787, 797, 808, 818, 828, 838, 848, 858, 868, 878, 888, 898, 909, 919, 929, 939, 949, 959, 969, 979, 989, 999]
Published 60 original articles · won praise 6 · views 1330

Guess you like

Origin blog.csdn.net/weixin_45775963/article/details/103733411