Discuss / Python / 膜拜大神的一行代码

膜拜大神的一行代码

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

直接用str可以将整数转化为字符串,然后切片比较。

  1. 切片的使用;
  2. 字符也可以直接用==比较。

偶一5我

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

性能不行。

你这个最简洁,比我的强.

在此插入代码

def str_sym(s): #字符串是否对称 n = len(s) b = True i = 0 while i < n: b = b*(s[i]==s[-(i+1)]) i = i +1 return b

def ispalindrome(n): str_n = str(n) # int转换成字符串 return str_sym(str_n)

output = filter(ispalindrome, range(1,1000)) print(list(output))


  • 1

Reply