天天看點

Python tip 題目筆記

翻轉字元串  x = '123456'   y = x[::-1]

給你一字典a,如a={1:1,2:2,3:3},輸出字典a的key,以','連結,如‘1,2,3'。            print ','.join([str(i) for i in a])

      
給你一個字元串 a, 輸出字元奇數位置的字元串。如a=‘12345’,則輸出135。               print a[::2]

列印100以内所有素數                                                            print ' '.join(['%s' % x for x in range(2,100) if not [y for y in range(2,x/2+1) if x % y == 0]])    list 生成器用法,點贊

列印L中位數                                                                   l = sorted(L); print ((l[len(L)/2] + l[len(L)/2-1]) /2.0) if len(L)%2 == 0 else l[len(L)/2]

最大公約數   
1.可能遺漏了a,b大小的初始      
while b:
    a, b = b, a%b
print a
           

  2.lambda 遞歸

gcd = lambda x,y: gcd(y,x) if x < y else gcd(y,x%y) if x%y else y

print gcd(a,b)      

轉載于:https://www.cnblogs.com/briller/p/4073364.html