首页 > 代码库 > Python小记

Python小记

赛选回文数

def is_palindrome(num):
    return num > 9 and str(num) == str(num)[::-1]
output = filter(is_palindrome,range(1,10000))
print(list(output)) 

 对L = [(‘Bob‘, 75), (‘Adam‘, 92), (‘Bart‘, 66), (‘Lisa‘, 88)]分别按名字和分数排序

L = [(Bob, 75), (Adam, 92), (Bart, 66), (Lisa, 88)]
def by_name(t):
    return t[0]
def by_score(t):
    return t[1]
print(sorted(L,key=by_score)) #[(‘Bart‘, 66), (‘Bob‘, 75), (‘Lisa‘, 88), (‘Adam‘, 92)]
print(sorted(L,key=by_name)) #[(‘Adam‘, 92), (‘Bart‘, 66), (‘Bob‘, 75), (‘Lisa‘, 88)]

#print(sorted(L,key=lambda x:x[0]))
#print(sorted(L,key=lambda x:x[1]))
 

 

Python小记