Python社区  »  Python

1 行 Python 代码能干哪些事,这 13 个你知道吗?

进击的Coder • 3 月前 • 69 次点击  

Python 之禅有一句话叫 “Simple is better than complex.”,简单,到底能多简单,一行代码?

你别说,一行代码还真能干不少事,这次转载一篇有意思的文章,来看看。

转载来源

博客:程序师

阅读本文大概需要 5 分钟。


首先你要了解一下 Python 之禅,一行代码输出“The Zen of 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!"""

从“The Zen of Python”也能看出,Python 倡导 Beautiful、Explicit、Simple 等原则,当然我们接下来要介绍的一行 Python 能实现哪些好玩的功能,可能和 Explicit 原则相违背。

1. 一行代码启动一个 Web 服务

python -m SimpleHTTPServer 8080  # python2python3 -m http.server 8080  # python3

2. 一行代码实现变量值互换

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

3. 一行代码解决 FizzBuzz 问题

FizzBuzz问题:打印数字1到100, 3的倍数打印“Fizz”, 5的倍数打印“Buzz”, 既是 3 又是 5 的倍数的打印“FizzBuzz”

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

4. 一行代码输出特定字符”Love”拼成的心形

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


veLoveLov veLoveLov
eLoveLoveLoveLove eLoveLoveLoveLove
veLoveLoveLoveLoveLoveLoveLoveLoveLoveLov
veLoveLoveLoveLoveLoveLoveLoveLoveLoveLoveL
veLoveLoveLoveLoveLoveLoveLoveLoveLoveLoveLov
eLoveLoveLoveLoveLoveLoveLoveLoveLoveLoveLove
LoveLoveLoveLoveLoveLoveLoveLoveLoveLoveLoveL
oveLoveLoveLoveLoveLoveLoveLoveLoveLoveLoveLo
veLoveLoveLoveLoveLoveLoveLoveLoveLoveLoveLov
eLoveLoveLoveLoveLoveLoveLoveLoveLoveLoveLove
oveLoveLoveLoveLoveLoveLoveLoveLoveLoveLove
eLoveLoveLoveLoveLoveLoveLoveLoveLoveLove
LoveLoveLoveLoveLoveLoveLoveLoveLoveLoveL
eLoveLoveLoveLoveLoveLoveLoveLoveLove
oveLoveLoveLoveLoveLoveLoveLoveLove
eLoveLoveLoveLoveLoveLoveLoveLove
veLoveLoveLoveLoveLoveLoveLov
oveLoveLoveLoveLoveLoveLo
LoveLoveLoveLoveLoveL
LoveLoveLoveLov
LoveLoveL
Lov
v

5. 一行代码输出 Mandelbrot 图像

Mandelbrot 图像:图像中的每个位置都对应于公式 N=x+y*i 中的一个复数

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

6. 一行代码打印九九乘法表

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 
1*2=2  2*2=4 
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81

7. 一行代码计算出 1-100 之间的素数(两个版本)

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))]))print(' '.join([str(item) for item in filter(lambda x: all(map(lambda p: x % p != 0, range(2, x))), range(2, 101))]))
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

8. 一行代码输出斐波那契数列

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)]])
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040]

9. 一行代码实现快排算法

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. 一行代码解决八皇后问题

[__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)))]
Q.......
....Q...
.......Q
.....Q..
..Q.....
......Q.
.Q......
...Q....
========
Q.......
.....Q..
.......Q
..Q.....
......Q.
...Q....
.Q......
....Q...
========
...

11. 一行代码实现数组的 flatten 功能: 将多维数组转化为一维

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

12. 一行代码实现 list, 有点类似与上个功能的反功能

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

13. 一行代码实现求解 2 的 1000 次方的各位数之和

print(sum(map(int, str(2**1000))))
1366


推荐阅读

1

跟繁琐的命令行说拜拜!Gerapy分布式爬虫管理框架来袭!

2

跟繁琐的模型说拜拜!深度学习脚手架 ModelZoo 来袭!

3

只会用Selenium爬网页?Appium爬App了解一下

4‍‍

妈妈再也不用担心爬虫被封号了!手把手教你搭建Cookies池


崔庆才

静觅博客博主,《Python3网络爬虫开发实战》作者

隐形字

个人公众号:进击的Coder

长按识别二维码关注

这里“阅读原文”,查看更多

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/33052
 
69 次点击  
分享到微博