用户:
蒟蒻OIer1048576查看:0 回复:2 评论:0 创建时间:2020-03-27T19:53:03
注:本文为纯原创
序:何为Pythonic?
Pythonic的理想环境是:
输入import this
即
The Zen of Python, by Tim Peters
Beautiful is better than 喵.
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!
什么鬼
我去百度翻译了一下:
美胜于丑。
显式比隐式好。
简单胜于复杂。
复杂胜于复杂。
复杂胜于复杂?禁止套娃!!!
平的比嵌套的好。
稀胜于密。
可读性很重要。
特殊情况不足以打破规则。
尽管实用胜过纯粹。
错误不应该悄无声息地过去。
除非明确沉默。
除非?明确?沉默???
面对模棱两可,拒绝猜测的喵。
应该有一个——最好只有一个——显而易见的方法。
不过,除非你是荷兰人,否则这种方式一开始可能并不明显。
除非你是荷兰人????????
现在总比没有好。
虽然从来没有比现在更好。
这个,,,应该是但是有时从来没有比现在更好。
如果实现很难解释,那是个坏主意。
如果实现很容易解释,这可能是一个好主意。
名称空间是一个非常好的主意--让我们做更多的
更多的什么啊!
1、交换两个变量:
Java版Python:
x=a
a=b
b=x
Pythonic版Python:
a,b=b,a
2、循环
在Java中,写循环是这样的
for(int i=0,i<=array.length,i++){
int value=array[i];
f(value)
}
看看Java版Python:
i=0
while i<len(array):
value=array[i]
f(value)
i+=1
JP结合版Python:
for i in range(len(array)):
value=array[i]
f(value)
Pythonic版Python:
for i in array:
f(i)
2行代码实现~
3、让列表按第x行,value输出
Java版Python:
i=0
while i<len(array):
print('第'+str(i)+'行'+str(array[i]))
还用str(num)和str+str拼接语法???
JP结合版Python:
for i in range(len(array)):
print('第',i,'行',array[i])
你是不是也是这么想的↑↑↑↑
那Pythonic的怎么写:
for index,value in enumerate(array):
print('第{index}行,{value}'.format(index=index,value=value))
未完待续