点击上方 "程序员小乐"关注公众号, 星标或置顶一起成长
每天早上8点20分, 第一时间与你相约
每日英文
Don't forget what to do and where to go you have promised yourself,no matter how difficult or far it is.
别忘了答应自己要做的事情,别忘了答应自己要去的地方,无论有多难,有多远。
每日掏心话
你若光明,这世界就不会黑暗。你若心怀希望,这世界就不会彻底绝望。你如不屈服,这世界又能把你怎样。
责编:乐乐
链接:techug.com/post/what-can-a-line-of-python-code-do.html
程序员小乐(ID:study_tech)第 638 次推文 图片来自网络
往日回顾:VS Code为什么能这么牛?
正文
首先你要了解一下Python之禅,一行代码输出“The Zen of Python”:
python -c "import this""""The Zen of Python, by Tim PetersBeautiful 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原则相违背。
如果你有其他这方面的小例子,也欢迎评论,我会加到文章中,文章也许会长期更新。
python -m SimpleHTTPServer 8080 # python2python3 -m http.server 8080 # python3
a, b = 1, 2; a, b = b, a
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( .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)]))
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))(0, 0.02*x+0.05j*y, 40)) < 2 else for x in range(-80, 20)]) for y in range(-20, 20)]))
6. 一行代码打印九九乘法表
print( .join([ .join([ %s*%s=%-2s % (y, x, x*y) for y in range(1, x+1)]) for x in range(1, 10)]))
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))]))
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)]])
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-1) for 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+3] for i in range(0, len(x), 3)]
print(sum(map(int, str(2**1000))))
欢迎在留言区留下你的观点,一起讨论提高。如果今天的文章让你有新的启发,学习能力的提升上有新的认识,欢迎转发分享给更多人。
欢迎各位读者加入程序员小乐技术群,在公众号后台回复“加群”或者“学习”即可。
猜你还想看
阿里、腾讯、百度、华为、京东最新面试题汇集
GitHub 标星3.5W+,超实用技术面试手册,从工作申请、面试考题再到优势谈判
漫画:一位“坑人”的编程大师
Redis基础都不会,好意思出去面试?
理解MySQL锁和事务,看这篇如何?
文章有问题?点此查看未经处理的缓存