一行神奇python

讓我們用一行代碼畫一個Mandelbrot:

>>> print'\n'.join([''.join(['*'if abs((lambda a:lambda z,c,n:a(a,z,c,n))(lambda s,z,c,n:z if n==0else s(s,z*z+c,c,n-1))(0,0.02*x+0.05j*y,40))<2 else' 'for x in range(-80,20)])for y in range(-20,20)])
  • 1
  • 1

這裏寫圖片描述

高效

對於隨手小工具而言,更是Python的拿手好戲。

一行代碼打印九九乘法表:

print '\n'.join([' '.join(['%s*%s=%-2s' % (y,x,x*y) for y in range(1,x+1)]) for x in range(1,10)])
  • 1
  • 1

輸出: 
乘法表

一行代碼計算出1-1000之間的素數

print(*(i for i in range(2, 1000) if all(tuple(i%j for j in range(2, int(i**.5))))))  
  • 1
  • 1

一行代碼可以輸出前100項斐波那契數列的值:

print [x[0] for x in [  (a[i][0], a.append((a[i][1], a[i][0]+a[i][1]))) for a in ([[1,1]], ) for i in xrange(100) ]]
  • 1
  • 1

一行代碼實現階乘,而且還帶交互:

>>> reduce ( lambda x,y:x*y,  range(1,input()+1))
10
3628800
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

一行代碼實現攝氏度與華氏度之間的轉換器:

>>> print((lambda i:i not in [1,2] and "Invalid input!" or i==1 and (lambda f:f<-459.67 and "Invalid input!" or f)(float(input("Please input a Celsius temperature:"))*1.8+32) or i==2 and (lambda c:c<-273.15 and "Invalid input!" or c)((float(input("Please input a Fahrenheit temperature:"))-32)/1.8))(int(input("1,Celsius to Fahrenheit\n2,Fahrenheit to Celsius\nPlease input 1 or 2\n"))))
1,Celsius to Fahrenheit
2,Fahrenheit to Celsius
Please input 1 or 2
1
Please input a Celsius temperature:28
82.4
>>> 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

至於字符串排序和快速排序更是手到擒來。

"".join((lambda x:(x.sort(),x)[1])(list(‘string’)))

qsort = lambda arr: len(arr) > 1 and  qsort(filter(lambda x: x<=arr[0], arr[1:] )) + arr[0:1] + qsort(filter(lambda x: x>arr[0], arr[1:] )) or arr
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

內涵

看一看下面一行python代碼,可能就要暈了:

猜數遊戲

這是原來爲了激發孩子編程興趣,讓孩子練習的代碼,它的真實面貌是大致這樣的:

def guess_my_number(n):
    while True:
        user_input = raw_input("Enter a positive integer to guess: ")
        if len(user_input)==0 or not user_input.isdigit():
            print "Not a positive integer!"
        else:
            user_input = int(user_input)
            if user_input > n:
                print "Too big ! Try again!"
            elif user_input < n:
                print "Too small ! Try again!"
            else:
                print "You win!"
                return True
guess_my_number(42)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

實際上,只要理解了函數式編程,使用神奇的Lambda,配合列表推導以及複雜一點的判斷語句,任何的python 代碼都可以轉換成一行代碼的。

例如,取一個列表裏的隨機數

import random as rnd
print rnd.choice([2,3, 5,7, 11,13,17])
  • 1
  • 2
  • 1
  • 2

轉換成Lambda 可以是:

print (lambda rnd: rnd.choice([1, 2, 3, 10]))(__import__('random'))
  • 1
  • 1

這些代碼出了覺得好玩,主要是可以幫助我們瞭解某些Python的雕蟲小記,尤其是神奇的Lambda 用法

延展

當然,還有其他好玩的地方,輸入下面這一行

import antigravity
  • 1
  • 1

它打開了瀏覽器,展示網站上的漫畫和相關內容: 
python 打開瀏覽器

我們可以把python的文件打包,做成庫的形式,然後import進來,是一種偷換概念和前提的一行代碼。例如,爲了與windows 傳輸文件,再Mac上臨時搭個ftp:

$ python -m pyftpdlib
  • 1
  • 1

這當然要依賴pyftpdlib 這個庫了,機器上沒有,pip install pyftpdlib 就可以了。

如果一行代碼中允許分號存在,那就只是犧牲可讀性而已了,那就基本上無所不能。

在連網的前提下,獲取公網IP地址

python -c "import socket; sock=socket.create_connection(('ns1.dnspod.net',6666)); print sock.recv(16); sock.close()"
  • 1
  • 1

一行代碼就可以輕易寫個小遊戲了,來模擬一下golf擊球。

python -c "import math as m;a,v=eval(input());[print('%03d'%x+' '*m.floor(0.5+x*m.tan(a)-x*x/(v*m.cos(a)))+'o') for x in range(102)]"
  • 1
  • 2
  • 1
  • 2

輸入角度和力量大小如(0.8,80),就能得到一條字符描畫的拋物線了。 
增加上while 等語句,畫一個沒完沒了的python -c "while 1:import random;print(random.choice('╱╲'), end='')"。 
maze wall

最後,一行代碼以python 的哲學結束吧。

$ python -c "import this" 
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章