猫史档案馆


Pythonic有更新

用户:蒟蒻OIer1048576蒟蒻OIer1048576查看:3 回复:5 评论:3 创建时间:2020-03-30T09:21:35


注:本文为纯原创

序:何为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_编程猫_紧张

我去百度翻译了一下:

美胜于丑。

显式比隐式好。

简单胜于复杂。

复杂胜于复杂。

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

平的比嵌套的好。

稀胜于密。

可读性很重要。

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

尽管实用胜过纯粹。

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

除非明确沉默。

除非?明确?沉默???

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

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

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

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

现在总比没有好。

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

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

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

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

名称空间是一个非常好的主意--让我们做更多的

更多的什么啊!

 

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))

 4、1~100所有奇数的平方列表

Java版Python:

list_=[]
for i in range(100):
    if i%2=1:
        list_.append(i**2)

那Pythonic的Python四肿么写的:

list_=[i**2 for i in range(100) if i%2==1]

5、文件读写

Java版Python:

try:
    f=open('在我这填路径','w/r')
    data=f.read()或f.write(data)
finally:
    f.close()

Pythonic版Python:

with open('在我这填路径','w/r') as f:
    data=f.read()或f.write(data)

6、分配

Java版Python

list1=['社区喵','凤喵','TUBG','007']
a=list1[0]
b=list1[1]
c=list1[2]
d=list1[3]

Pythonic版Python

a,b,c,d=list1

7、我实在不想更新了

UHl0aG9uIGlzIGdvb2QsMTA0ODU3NiBpcyBnb29k(Base喵)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


回复

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

考古

点赞0


评论


蒟蒻OIer1048576蒟蒻OIer1048576

考古

点赞0


评论


蒟蒻OIer1048576蒟蒻OIer1048576

/ۡۙۨۙۤۢ۠ۢۚۨۨ۠ۖۚ۫ۘۙۙۡۡۤۖۧۖۛۨ/ۡۙۨۙۤۢ۠ۢۚۨۨ۠ۖۚ۫ۘۙۙۡۡۤۖۧۖۛۨ/ۡۙۨۙۤۢ۠ۢۚۨۨ۠ۖۚ۫ۘۙۙۡۡۤۖۧۖۛۨ/ۡۙۨۙۤۢ۠ۢۚۨۨ۠ۖۚ۫ۘۙۙۡۡۤۖۧۖۛۨ/ۡۙۨۙۤۢ۠ۢۚۨۨ۠ۖۚ۫ۘۙۙۡۡۤۖۧۖۛۨ/ۡۙۨۙۤۢ۠ۢۚۨۨ۠ۖۚ۫ۘۙۙۡۡۤۖۧۖۛۨ/ۡۙۨۙۤۢ۠ۢۚۨۨ۠ۖۚ۫ۘۙۙۡۡۤۖۧۖۛۨ/ۡۙۨۙۤۢ۠ۢۚۨۨ۠ۖۚ۫ۘۙۙۡۡۤۖۧۖۛۨ/ۡۙۨۙۤۢ۠ۢۚۨۨ۠ۖۚ۫ۘۙۙۡۡۤۖۧۖۛۨ/ۡۙۨۙۤۢ۠ۢۚۨۨ۠ۖۚ۫ۘۙۙۡۡۤۖۧۖۛۨ/ۡۙۨۙۤۢ۠ۢۚۨۨ۠ۖۚ۫ۘۙۙۡۡۤۖۧۖۛۨ/ۡۙۨۙۤۢ۠ۢۚۨۨ۠ۖۚ۫ۘۙۙۡۡۤۖۧۖۛۨ/ۡۙۨۙۤۢ۠ۢۚۨۨ۠ۖۚ۫ۘۙۙۡۡۤۖۧۖۛۨ/ۡۙۨۙۤۢ۠ۢۚۨۨ۠ۖۚ۫ۘۙۙۡۡۤۖۧۖۛۨ/ۡۙۨۙۤۢ۠ۢۚۨۨ۠ۖۚ۫ۘۙۙۡۡۤۖۧۖۛۨ/ۡۙۨۙۤۢ۠ۢۚۨۨ۠ۖۚ۫ۘۙۙۡۡۤۖۧۖۛۨ/ۡۙۨۙۤۢ۠ۢۚۨۨ۠ۖۚ۫ۘۙۙۡۡۤۖۧۖۛۨ/ۡۙۨۙۤۢ۠ۢۚۨۨ۠ۖۚ۫ۘۙۙۡۡۤۖۧۖۛۨ/ۡۙۨۙۤۢ۠ۢۚۨۨ۠ۖۚ۫ۘۙۙۡۡۤۖۧۖۛۨ/ۡۙۨۙۤۢ۠ۢۚۨۨ۠ۖۚ۫ۘۙۙۡۡۤۖۧۖۛۨ/ۡۙۨۙۤۢ۠ۢۚۨۨ۠ۖۚ۫ۘۙۙۡۡۤۖۧۖۛۨ/ۡۙۨۙۤۢ۠ۢۚۨۨ۠ۖۚ۫ۘۙۙۡۡۤۖۧۖۛۨ/ۡۙۨۙۤۢ۠ۢۚۨۨ۠ۖۚ۫ۘۙۙۡۡۤۖۧۖۛۨ

点赞0


评论


Albert钟Albert钟

挖坟来辽

点赞1


评论


伴雪纷飞伴雪纷飞

我来解那个base喵了  :Python is good,1048576 is good

内心:(?!啥玩意?) 

点赞0


评论