猫史档案馆


Pythonic

用户:蒟蒻OIer1048576蒟蒻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!

什么鬼emotion_编程猫_紧张

我去百度翻译了一下:

美胜于丑。

měi shèng chǒu

Explicit is better than implicit.

显式比隐式好。

xiǎn shì yǐn shì hǎo

Simple is better than complex.

简单胜于复杂。

jiǎn dān shèng

Complex is better than complicated.

复杂胜于复杂。

复杂胜于复杂?禁止套娃!!!

shèng

Flat is better than nested.

平的比嵌套的好。

píng de qiàn tào de hǎo

Sparse is better than dense.

稀胜于密。

shèng

Readability counts.

可读性很重要。

xìng hěn zhòng yào

Special cases aren't special enough to break the rules.

特殊情况不足以打破规则。

shū qíng kuàng guī

Although practicality beats purity.

尽管实用胜过纯粹。

jǐn guǎn shí yòng shèng guò chún cuì

Errors should never pass silently.

错误不应该悄无声息地过去。

cuò yīng gāi qiǎo shēng de guò

Unless explicitly silenced.

除非明确沉默。

除非?明确?沉默???

chú fēi míng què chén

In the face of ambiguity, refuse the temptation to guess.

面对模棱两可,拒绝猜测的喵。

miàn duì léng liǎng jué cāi de yòu huò

There should be one-- and preferably only one --obvious way to do it.

应该有一个——最好只有一个——显而易见的方法。

yīng gāi yǒu zuì hǎo zhǐ yǒu xiǎn ér jiàn de fāng

Although that way may not be obvious at first unless you're Dutch.

不过,除非你是荷兰人,否则这种方式一开始可能并不明显。

除非你是荷兰人????????

guò chú fēi shì lán rén fǒu zhè zhǒng fāng shì kāi shǐ néng bìng míng xiǎn

Now is better than never.

现在总比没有好。

xiàn zài zǒng méi yǒu hǎo

Although never is often better than *right* now.

虽然从来没有比现在更好。

这个,,,应该是但是有时从来没有比现在更好。

suī rán cóng lái méi yǒu xiàn zài gèng hǎo

If the implementation is hard to explain, it's a bad idea.

如果实现很难解释,那是个坏主意。

guǒ shí xiàn hěn nán jiě shì shì huài zhǔ yi

If the implementation is easy to explain, it may be a good idea.

如果实现很容易解释,这可能是一个好主意。

guǒ shí xiàn hěn róng jiě shì zhè néng shì hǎo zhǔ yi

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拼接语法???emotion_雷电猴_捂嘴笑

 JP结合版Python:

for i in range(len(array)):
    print('第',i,'行',array[i])

 你是不是也是这么想的↑↑↑↑emotion_雷电猴_举手

那Pythonic的怎么写:

for index,value in enumerate(array):
    print('第{index}行,{value}'.format(index=index,value=value))

 未完待续


回复

上一页1 页 / 共 1下一页
蒟蒻OIer1048576蒟蒻OIer1048576

emotion_编程猫_加油emotion_编程猫_加油emotion_编程猫_加油emotion_编程猫_加油emotion_编程猫_加油emotion_编程猫_加油emotion_编程猫_加油emotion_编程猫_加油emotion_编程猫_加油emotion_编程猫_加油emotion_编程猫_加油emotion_编程猫_加油emotion_编程猫_加油emotion_编程猫_加油emotion_编程猫_加油emotion_编程猫_加油emotion_编程猫_加油emotion_编程猫_加油emotion_编程猫_加油emotion_编程猫_加油emotion_编程猫_加油emotion_编程猫_加油emotion_编程猫_加油emotion_编程猫_加油emotion_编程猫_加油emotion_编程猫_加油

点赞0


评论


星空之忆星空之忆

emmmm

点赞0


评论