偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

幾個小例子告訴你, 一行Python代碼能干哪些事

開發(fā) 后端
從“The Zen of Python”能看出,Python倡導(dǎo)Beautiful、Explicit、Simple等原則,當(dāng)然我們接下來要介紹的一行Python能實現(xiàn)哪些好玩的功能,可能和Explicit原則相違背。

首先你要了解一下Python之禪,一行代碼輸出“The Zen of Python”:

 

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

從“The Zen of Python”也能看出,Python倡導(dǎo)Beautiful、Explicit、Simple等原則,當(dāng)然我們接下來要介紹的一行Python能實現(xiàn)哪些好玩的功能,可能和Explicit原則相違背。

[[207722]]

如果你有其他這方面的小例子,也歡迎評論,我會加到文章中,文章也許會長期更新。

(1)一行代碼啟動一個Web服務(wù)

 

  1. python -m SimpleHTTPServer 8080 # python2  
  2. python3 -m http.server 8080 # python3 

 

幾個小例子告訴你, 一行Python代碼能干哪些事

(2)一行代碼實現(xiàn)變量值互換

  1. a, b = 1, 2; a, b = b, a 

(3)一行代碼解決FizzBuzz問題:

FizzBuzz問題:打印數(shù)字1到100, 3的倍數(shù)打印“Fizz”, 5的倍數(shù)打印“Buzz”, 既是3又是5的倍數(shù)的打印“FizzBuzz”

  1. for x in range(1, 101): print("fizz"[x % 3 * 4:]+"buzz"[x % 5 * 4:] or x) 

(4)一行代碼輸出特定字符”Love”拼成的心形

  1. print('\n'.join([''.join([('Love'[(x-y) % len('Love')] 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(30, -30, -1)])) 

 

幾個小例子告訴你, 一行Python代碼能干哪些事

(5)一行代碼輸出Mandelbrot圖像

Mandelbrot圖像:圖像中的每個位置都對應(yīng)于公式N=x+y*i中的一個復(fù)數(shù)

  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 == 0 else 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)])) 

 

[[207723]]

(6)一行代碼打印九九乘法表

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

 

幾個小例子告訴你, 一行Python代碼能干哪些事

(7)一行代碼計算出1-100之間的素數(shù)(兩個版本)

 

  1. print(' '.join([str(item) for item in filter(lambda x: not [x % i for i in range(2, x) if x % i == 0], range(2, 101))]))  
  2. print(' '.join([str(item) for item in filter(lambda x: all(map(lambda p: x % p != 0, range(2, x))), range(2, 101))])) 

 

幾個小例子告訴你, 一行Python代碼能干哪些事

(8)一行代碼輸出斐波那契數(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 range(30)]]) 

 

幾個小例子告訴你, 一行Python代碼能干哪些事

(9)一行代碼實現(xiàn)快排算法

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

(10)一行代碼解決八皇后問題

  1. [__import__('sys').stdout.write('\n'.join('.' * i + 'Q' + '.' * (8-i-1) for i in vec) + "\n========\n"for vec in __import__('itertools').permutations(range(8)) if 8 == len(set(vec[i]+i for i in range(8))) == len(set(vec[i]-i for i in range(8)))] 

 

幾個小例子告訴你, 一行Python代碼能干哪些事

(11)一行代碼實現(xiàn)數(shù)組的flatten功能: 將多維數(shù)組轉(zhuǎn)化為一維

  1. flatten = lambda x: [y for l in x for y in flatten(l)] if isinstance(x, list) else [x] 

(12)一行代碼實現(xiàn)list, 有點類似與上個功能的反功能

  1. array = lambda x: [x[i:i+3] for i in range(0, len(x), 3)] 

(13)一行代碼實現(xiàn)求解2的1000次方的各位數(shù)之和

  1. print(sum(map(int, str(2**1000)))) 
責(zé)任編輯:未麗燕 來源: 程序師
相關(guān)推薦

2019-05-07 08:58:53

Python代碼Web

2016-12-02 08:53:18

Python一行代碼

2022-02-23 14:37:48

代碼Pythonbug

2022-04-09 09:11:33

Python

2021-10-27 10:30:04

Python字符串代碼

2021-05-28 07:39:17

SQL代碼操作

2017-04-13 19:20:18

Python代碼并行任務(wù)

2021-11-02 16:25:41

Python代碼技巧

2020-08-19 10:30:25

代碼Python多線程

2021-10-29 10:38:20

代碼 PILPython

2020-08-24 08:25:48

Python開發(fā)工具

2020-08-12 14:54:00

Python代碼開發(fā)

2020-09-28 12:34:38

Python代碼開發(fā)

2015-03-20 14:51:09

Testin云測

2020-02-19 15:02:23

代碼開發(fā)工具

2017-04-05 11:10:23

Javascript代碼前端

2014-02-12 13:43:50

代碼并行任務(wù)

2021-08-23 17:49:02

代碼開發(fā)模型

2024-08-08 09:15:08

SQL代碼復(fù)制表

2018-09-19 15:46:51

編程語言Python編譯器
點贊
收藏

51CTO技術(shù)棧公眾號