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

2019 年 5 月 5 日 机器学习算法与Python学习



来源:http://www.techug.com/post/what-can-a-line-of-python-code-do.html


首先你要了解一下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  # python2
python3 -m http.server 8080  # python3



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

  
  
    
a, b = 12; a, b = b, a


3. 一行代码解决FizzBuzz问题

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

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


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

print( .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(-3030)]) for y in range(30-30-1)]))


5. 一行代码输出Mandelbrot图像

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

  
  
    
 print( .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))(00.02*x+0.05j*y, 40)) < 2 else     for x in range(-8020)]) for y in range(-2020)]))



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

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


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(2101))]))
print(   .join([str(item) for item in filter(lambda x: all(map(lambda p: x % p != 0, range(2, x))), range(2101))]))



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

print([x[0for x in [(a[i][0], a.append([a[i][1], a[i][0]+a[i][1]])) for a in ([[11]], ) for i in range(30)]])


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( .join( .  * i +  Q  +  .  * (8-i-1for i in vec) + "========"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)))]


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+3for i in range(0, len(x), 3)]


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

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


推荐阅读

在阿里“解放”鉴黄师是一种怎样的体验

3 个适合新人上手的Python项目

不读研,没出路?全国最疯狂考研地区榜

一个牛逼的 Python 调试工具

8 行代码用Python画一个中国地图

喜欢就点击“在看”吧!
登录查看更多
0

相关内容

【实用书】学习用Python编写代码进行数据分析,103页pdf
专知会员服务
190+阅读 · 2020年6月29日
一份简明有趣的Python学习教程,42页pdf
专知会员服务
76+阅读 · 2020年6月22日
【2020新书】如何认真写好的代码和软件,318页pdf
专知会员服务
63+阅读 · 2020年3月26日
【干货书】流畅Python,766页pdf,中英文版
专知会员服务
223+阅读 · 2020年3月22日
算法与数据结构Python,369页pdf
专知会员服务
160+阅读 · 2020年3月4日
《代码整洁之道》:5大基本要点
专知会员服务
49+阅读 · 2020年3月3日
【新书】傻瓜式入门深度学习,371页pdf
专知会员服务
183+阅读 · 2019年12月28日
手把手教你用Python实现“坦克大战”,附详细代码!
机器学习算法与Python学习
11+阅读 · 2019年6月8日
一个牛逼的 Python 调试工具
机器学习算法与Python学习
15+阅读 · 2019年4月30日
GitHub 热门:别再用 print 输出来调试代码了
Python开发者
27+阅读 · 2019年4月24日
如何编写完美的 Python 命令行程序?
CSDN
5+阅读 · 2019年1月19日
Python为啥这么牛?
Python程序员
3+阅读 · 2018年3月30日
为什么你应该学 Python ?
计算机与网络安全
4+阅读 · 2018年3月24日
细数10个隐藏在Python中的彩蛋
七月在线实验室
4+阅读 · 2018年1月16日
手把手教TensorFlow(附代码)
深度学习世界
15+阅读 · 2017年10月17日
代码这样写不止于优雅(Python版)
数说工作室
4+阅读 · 2017年7月17日
CoCoNet: A Collaborative Convolutional Network
Arxiv
6+阅读 · 2019年1月28日
Attend More Times for Image Captioning
Arxiv
6+阅读 · 2018年12月8日
A Survey on Deep Transfer Learning
Arxiv
11+阅读 · 2018年8月6日
Arxiv
11+阅读 · 2018年5月13日
VIP会员
相关VIP内容
【实用书】学习用Python编写代码进行数据分析,103页pdf
专知会员服务
190+阅读 · 2020年6月29日
一份简明有趣的Python学习教程,42页pdf
专知会员服务
76+阅读 · 2020年6月22日
【2020新书】如何认真写好的代码和软件,318页pdf
专知会员服务
63+阅读 · 2020年3月26日
【干货书】流畅Python,766页pdf,中英文版
专知会员服务
223+阅读 · 2020年3月22日
算法与数据结构Python,369页pdf
专知会员服务
160+阅读 · 2020年3月4日
《代码整洁之道》:5大基本要点
专知会员服务
49+阅读 · 2020年3月3日
【新书】傻瓜式入门深度学习,371页pdf
专知会员服务
183+阅读 · 2019年12月28日
相关资讯
手把手教你用Python实现“坦克大战”,附详细代码!
机器学习算法与Python学习
11+阅读 · 2019年6月8日
一个牛逼的 Python 调试工具
机器学习算法与Python学习
15+阅读 · 2019年4月30日
GitHub 热门:别再用 print 输出来调试代码了
Python开发者
27+阅读 · 2019年4月24日
如何编写完美的 Python 命令行程序?
CSDN
5+阅读 · 2019年1月19日
Python为啥这么牛?
Python程序员
3+阅读 · 2018年3月30日
为什么你应该学 Python ?
计算机与网络安全
4+阅读 · 2018年3月24日
细数10个隐藏在Python中的彩蛋
七月在线实验室
4+阅读 · 2018年1月16日
手把手教TensorFlow(附代码)
深度学习世界
15+阅读 · 2017年10月17日
代码这样写不止于优雅(Python版)
数说工作室
4+阅读 · 2017年7月17日
相关论文
CoCoNet: A Collaborative Convolutional Network
Arxiv
6+阅读 · 2019年1月28日
Attend More Times for Image Captioning
Arxiv
6+阅读 · 2018年12月8日
A Survey on Deep Transfer Learning
Arxiv
11+阅读 · 2018年8月6日
Arxiv
11+阅读 · 2018年5月13日
Top
微信扫码咨询专知VIP会员