日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢(xún)
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
一行Python代碼

Life is short, just use Python.

創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站設(shè)計(jì)、網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿(mǎn)足客戶(hù)于互聯(lián)網(wǎng)時(shí)代的驛城網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

自從08年接觸Python,就有愛(ài)不釋手的感覺(jué),逐漸地,有些不忍地疏遠(yuǎn)了Perl 和Shell編程,因?yàn)閜ython 的優(yōu)雅么? 不全是,主要是可以高效開(kāi)發(fā)吧。

高效,那一行代碼可以干什么呢?

有趣

我孩子的英文名叫andy,也許當(dāng)初教他寫(xiě)程序的時(shí)候,如果先秀一下這行代碼,可能就更能激起他對(duì)代碼的興趣了。

 
 
  1. print'\n'.join([''.join([('AndyLove'[(x-y)%8]if((x*0.05)**2+(y*0.1)**2-1)**3-(x*0.05)**2*(y*0.1)**3<=0 else' ')for x in range(-30,30)])for y in range(15,-15,-1)])

在python 里執(zhí)行它,會(huì)輸出一個(gè)字符拼成的愛(ài)心。

LoveAndy

字符圖形還是很有趣的,有一個(gè)著名的圖像叫mandelbrot。Mandelbrot圖像中的每個(gè)位置都對(duì)應(yīng)于公式N=x+y*i 中的一個(gè)復(fù)數(shù),高中學(xué)過(guò)復(fù)數(shù)的都還應(yīng)該有印象。每個(gè)位置用參數(shù)N來(lái)表示,它是x*x+y*y的平方根。如果這個(gè)值大于或等于2,則這個(gè)數(shù)字對(duì)應(yīng)的位置值是0。如果參數(shù)N的值小于2,就把N的值改為NN-N(N=(xx-yy-x)+(2xy-y)i)),并再次測(cè)試這個(gè)新N值。wiki百科給出的圖像是這樣的:

Mandelbrot

讓我們用一行代碼畫(huà)一個(gè)Mandelbrot:

 
 
  1. 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)])

高效

對(duì)于隨手小工具而言,更是Python的拿手好戲。

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

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

輸出:

乘法表

一行代碼計(jì)算出1-1000之間的素?cái)?shù)

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

一行代碼可以輸出前100項(xiàng)斐波那契數(shù)列的值:

 
 
  1. 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) ]]

一行代碼實(shí)現(xiàn)階乘,而且還帶交互:

 
 
  1. reduce ( lambda x,y:x*y,  range(1,input()+1))103628800

一行代碼實(shí)現(xiàn)攝氏度與華氏度之間的轉(zhuǎn)換器:

 
 
  1. 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 Fahrenheit2,Fahrenheit to Celsius
  2. Please input 1 or 21Please input a Celsius temperature:2882.4

至于字符串排序和快速排序更是手到擒來(lái)。

 
 
  1. "".join((lambda x:(x.sort(),x)[1])(list(‘string’)))
  2. qsort = lambda arr: len(arr) > 1 and  qsort(filter(lambda x: x<=arr[0], arr[1:] )) + 

內(nèi)涵

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

這是《跟孩子一起學(xué)編程》一書(shū)中,為了激發(fā)孩子編程興趣,讓孩子練習(xí)的代碼——猜數(shù)游戲,它的真實(shí)面貌是大致這樣的:

 
 
  1. def guess_my_number(n):
  2.     while True:
  3.         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!"
  4.         else:
  5.             user_input = int(user_input)            if user_input > n:                print "Too big ! Try again!"
  6.             elif user_input < n:                print "Too small ! Try again!"
  7.             else:                print "You win!"
  8.                 return Trueguess_my_number(42)

神奇的Lambda,配合列表推導(dǎo)以及復(fù)雜一點(diǎn)的判斷語(yǔ)句,任何的python 代碼都可以轉(zhuǎn)換成一行代碼的。

例如,取一個(gè)列表里的隨機(jī)數(shù)

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

轉(zhuǎn)換成Lambda 可以是:

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

這些代碼出了覺(jué)得好玩,主要是可以幫助我們了解某些Python的特性技藝,尤其是神奇的Lambda 用法。

延展

當(dāng)然,還有其他好玩的地方,輸入下面這一行

import antigravity

它打開(kāi)了瀏覽器,展示網(wǎng)站上的漫畫(huà)和相關(guān)內(nèi)容:

python 打開(kāi)瀏覽器

我們可以把python的文件打包,做成庫(kù)的形式,然后import進(jìn)來(lái),是一種偷換概念和改變前提的一行代碼。例如,為了與windows 傳輸文件,在Mac上臨時(shí)搭個(gè)ftp服務(wù)器:

 
 
  1. $ python -m pyftpdlib

這當(dāng)然要依賴(lài)pyftpdlib 這個(gè)庫(kù)了,機(jī)器上沒(méi)有,pip install pyftpdlib 就可以了。

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

在線的時(shí)候,獲取公網(wǎng)IP地址的一行代碼:

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

一行代碼就可以輕易寫(xiě)個(gè)小游戲了,來(lái)模擬一下golf擊球。

 
 
  1. 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)]"

輸入角度和力量大小如(0.8,80),就能得到一條字符描畫(huà)的拋物線了。

增加上while 等語(yǔ)句,畫(huà)一個(gè)沒(méi)完沒(méi)了的墻紙python -c "while 1:import random;print(random.choice('╱╲'), end='')"。

maze wall

***, Zen of Python 以一行代碼來(lái)結(jié)束吧。

 
 
  1. $ python -c "import this"  
  2. The Zen of Python, by Tim Peters
  3. Beautiful is better than ugly.
  4. Explicit is better than implicit.
  5. Simple is better than complex.
  6. Complex is better than complicated.
  7. Flat is better than nested.
  8. Sparse is better than dense.
  9. Readability counts.
  10. Special cases aren't special enough to break the rules.
  11. Although practicality beats purity.
  12. Errors should never pass silently.
  13. Unless explicitly silenced.
  14. In the face of ambiguity, refuse the temptation to guess.
  15. There should be one-- and preferably only one --obvious way to do it.
  16. Although that way may not be obvious at first unless you're Dutch.
  17. Now is better than never.
  18. Although never is often better than *right* now.
  19. If the implementation is hard to explain, it's a bad idea.
  20. If the implementation is easy to explain, it may be a good idea.
  21. Namespaces are one honking great idea -- let's do more of those!

【本文來(lái)自專(zhuān)欄作者老曹的原創(chuàng)文章,作者微信公眾號(hào):喔家ArchiSelf,id:wrieless-com】


網(wǎng)頁(yè)標(biāo)題:一行Python代碼
文章URL:http://www.dlmjj.cn/article/dhigeso.html