Skip to content

Commit 9441818

Browse files
committed
add python_oneline.py
1 parent 7c37ad0 commit 9441818

File tree

3 files changed

+93
-6
lines changed

3 files changed

+93
-6
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
### python_lda.py: 玩点高级的--带你入门Topic模型LDA(小改进+附源码)
1414

1515
### python_sqlalchemy.py: 作为一个Pythoner,不会SQLAlchemy都不好意思跟同行打招呼!
16+
17+
### python_oneline.py: 几个小例子告诉你, 一行Python代码能干哪些事
1618
============================================================
1719

1820
### 您可以fork该项目,并在修改后提交Pull request

python_base.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,12 +1273,7 @@ def logger(self):
12731273

12741274

12751275
"""其他----其他----其他----其他----其他----其他----其他----其他----其他----其他----其他----其他----其他----其他----其他----其他----其他----其他----其他"""
1276-
1277-
#-- 60个字符解决FizzBuzz:
1278-
"""写一个程序, 打印数字1到100, 3的倍数打印“Fizz”来替换这个数, 5的倍数打印“Buzz”, 既是3又是5的倍数的打印“FizzBuzz”"""
1279-
for x in range(101):
1280-
print("fizz"[x%3*4::]+"buzz"[x%5*4::] or x) # 解释:最主要用到列表(字符串)的子表
1281-
1276+
12821277
#-- Python实现任意深度的赋值 例如a[0] = 'value1'; a[1][2] = 'value2'; a[3][4][5] = 'value3'
12831278
class MyDict(dict):
12841279
def __setitem__(self, key, value): # 该函数不做任何改动 这里只是为了输出

python_oneline.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# _*_ coding: utf-8 _*_
2+
3+
"""
4+
python_oneline.py by xianhu
5+
"""
6+
7+
8+
# 首先来个python之禅
9+
# python -c "import this"
10+
"""
11+
The Zen of Python, by Tim Peters
12+
13+
Beautiful is better than ugly.
14+
Explicit is better than implicit.
15+
Simple is better than complex.
16+
Complex is better than complicated.
17+
Flat is better than nested.
18+
Sparse is better than dense.
19+
Readability counts.
20+
Special cases aren't special enough to break the rules.
21+
Although practicality beats purity.
22+
Errors should never pass silently.
23+
Unless explicitly silenced.
24+
In the face of ambiguity, refuse the temptation to guess.
25+
There should be one-- and preferably only one --obvious way to do it.
26+
Although that way may not be obvious at first unless you're Dutch.
27+
Now is better than never.
28+
Although never is often better than *right* now.
29+
If the implementation is hard to explain, it's a bad idea.
30+
If the implementation is easy to explain, it may be a good idea.
31+
Namespaces are one honking great idea -- let's do more of those!
32+
"""
33+
34+
35+
# 一行代码启动一个Web服务
36+
# python -m SimpleHTTPServer 8080
37+
# python3 -m http.server 8080
38+
39+
40+
# 一行代码实现变量值互换
41+
a, b = 1, 2; a, b = b, a
42+
43+
44+
# 一行代码解决FizzBuzz问题: 打印数字1到100, 3的倍数打印“Fizz”来替换这个数, 5的倍数打印“Buzz”, 既是3又是5的倍数的打印“FizzBuzz”
45+
print(' '.join(["fizz"[x % 3 * 4:]+"buzz"[x % 5 * 4:] or str(x) for x in range(1, 101)]))
46+
47+
48+
# 一行代码输出特定字符"Love"拼成的心形
49+
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)]))
50+
51+
52+
# 一行代码输出Mandelbrot图像: Mandelbrot图像中的每个位置都对应于公式N=x+y*i中的一个复数
53+
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)]))
54+
55+
56+
# 一行代码打印九九乘法表
57+
print('\n'.join([' '.join(['%s*%s=%-2s' % (y, x, x*y) for y in range(1, x+1)]) for x in range(1, 10)]))
58+
59+
60+
# 一行代码计算出1-100之间的素数(两个版本)
61+
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))]))
62+
print(' '.join([str(item) for item in filter(lambda x: all(map(lambda p: x % p != 0, range(2, x))), range(2, 101))]))
63+
64+
65+
# 一行代码输出斐波那契数列
66+
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)]])
67+
68+
69+
# 一行代码实现快排算法
70+
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
71+
72+
73+
# 一行代码解决八皇后问题
74+
[__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)))]
75+
76+
77+
# 一行代码实现数组的flatten功能: 将多维数组转化为一维
78+
flatten = lambda x: [y for l in x for y in flatten(l)] if isinstance(x, list) else [x]
79+
80+
81+
# 一行代码实现list, 有点类似与上个功能的反功能
82+
array = lambda x: [x[i:i+3] for i in range(0, len(x), 3)]
83+
84+
85+
# 一行代码实现求解2的1000次方的各位数之和
86+
print(sum(map(int, str(2**1000))))
87+
88+
89+
# 最后推荐一篇文章: [Python One-liner Games](http://arunrocks.com/python-one-liner-games/)
90+
exit()

0 commit comments

Comments
 (0)