1. 程式人生 > >如何寫出一見傾心的Python程式碼?獲取小姐姐的歡心呢?

如何寫出一見傾心的Python程式碼?獲取小姐姐的歡心呢?

如何寫出一見傾心的Python程式碼?獲取小姐姐的歡心呢?

 

The Zen of Python, by Tim Peters
Beautiful 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!

翻譯版:

Python之禪,作者Tim Peters
優美勝於醜陋
明瞭勝於晦澀
簡單勝於複雜
複雜勝於雜亂
扁平勝於巢狀
間隔勝於緊湊
可讀性很重要
特例不足以特殊到違背這些原則
不要忽視錯誤,除非程式需要這樣做
面對模稜兩可,拒絕猜測
解決問題最直接的方法應該有一種,最好只有一種
可能這種方法一開始不夠直接,因為你不是範羅蘇姆
做也許好過不做,但不想就做還不如不做
如果方案難以描述明白,那麼一定是個糟糕的方案
如果容易描述,那麼可能是個好方案
名稱空間是一種絕妙的理念,多加利用

進群:“960410445”獲取原始碼!

你內心竊喜,原來Python還有這樣一個隱藏的密門。你開始揣摩並它的深意,接著你goole了這段文字的深意,認識到到pythonic這個詞。從此便一發不可收拾:

如何寫出一見傾心的Python程式碼?獲取小姐姐的歡心呢?

 

P

a = [u'凡', u'塵', u'皆', u'美']
if a.index(u'塵') < a.index(u'皆') < a.index(u'美'):
 print 'Yes'
else:
 print 'No'
>>> Yes

置 換 變 量

NP

a = u'凡塵皆美'
b = u'念念不忘'
temp = a
a = b
b = temp
>>> print a, b
>>> 念念不忘 凡塵皆美

P

a = u'凡塵皆美'
b = u'念念不忘'
a, b = b, a
>>> print a, b
>>> 念念不忘 凡塵皆美

迭 代 數 組

NP

arry = [u'凡', u'塵', u'皆', u'美']
for i in range(len(arry)):
 print i, arry[i]
>>>
0 凡
1 塵
2 皆
3 美

P

arry = [u'凡', u'塵', u'皆', u'美']
for i, j in enumerate(arry):
 print i, j
>>>
0 凡
1 塵
2 皆
3 美

字 符 拼 接

NP

chart_demo = u'凡塵皆美,念念不忘'
convert = chart_demo[0]
for i in chart_demo[1:]:
 convert += '_' + i
>>> print convert
>>> 凡_塵_皆_美_,_念_念_不_忘

P

chart_demo = u'凡塵皆美,念念不忘'
convert = '_'.join(chart_demo)
>>> print convert
>>> 凡_塵_皆_美_,_念_念_不_忘

列 表 生 成 式

NP

chart_demo = u'凡塵皆美,念念不忘'
result = []
for i in chart_demo:
 if i == u'念':
 result.append(i)
>>> print result
>>> ['念', '念']

P

chart_demo = u'凡塵皆美,念念不忘'
result = [i for i in chart_demo if i == u'念']
>>> print result
>>> ['念', '念']

布 爾 判 斷

NP

a = u'凡'
b = [u'凡']
c = {'fan': u'凡'}
if a != None and len(b) > 0 and c != {}:
 print '凡塵皆美'
>>> 凡塵皆美

P

a = u'凡'
b = [u'凡']
c = {'fan': u'凡'}
if a and b and c:
 print '凡塵皆美'
>>> 凡塵皆美

zip 建 字 典

NP

keys = ['a', 'b', 'c', 'd']
values = [u'凡', u'塵', u'皆', u'美']
dict_result = {}
for i,j in enumerate(keys):
 dict_result[j] = values[i]
>>> print dict_result
>>> {'a': '凡', 'b': '塵', 'c': '皆', 'd': '美'}

P

私信菜鳥007獲取更多案例原始碼!

keys = ['a', 'b', 'c', 'd']
values = [u'凡', u'塵', u'皆', u'美']
dict_result = dict(zip(keys, values))
>>> print dict_result
>>> {'a': '凡', 'b': '塵', 'c': '皆', 'd': '美'}

三 目 運 算 符

NP

chart_demo = u'凡塵皆美'
if len(chart_demo) > 3:
 temp = 'beautiful'
else:
 temp = 'ugly'
>>> print temp
>>> beautiful

P

chart_demo = u'凡塵皆美'
temp = 'beautiful' if len(chart_demo) > 3 else 'ugly'
>>> print temp
>>> beautiful

字 典 計 數

NP

chart_demo = u'凡塵皆美,念念不忘'
count = {}
for i in chart_demo:
 if not i in count:
 count[i] = 0
 count[i] += 1
>>> print count
>>> {'凡': 1, '塵': 1, '皆': 1, '美': 1, ',': 1, '念': 2, '不': 1, '忘': 1}

P

chart_demo = u'凡塵皆美,念念不忘'
No.1:
count = {}
for i in chart_demo:
 count[i] = count.get(i, 0) + 1
>>> print count
>>> {'凡': 1, '塵': 1, '皆': 1, '美': 1, ',': 1, '念': 2, '不': 1, '忘': 1}
No.2:
from collections import Counter
c = Counter(u'凡塵皆美,念念不忘')
>>> print c.most_common()
>>> [('念', 2), ('凡', 1), ('塵', 1), ('皆', 1), ('美', 1), (',', 1), ('不', 1), ('忘', 1)]

list 反 向 遍 歷

NP

arry = [u'凡', u'塵', u'皆', u'美']
for i in range(len(arry) - 1, -1, -1):
 print arry[i]
>>>
美
皆
塵
凡

P

arry = [u'凡', u'塵', u'皆', u'美']
No.1:
for i in reversed(arry):
 print i
NO.2:
for i in arry[::-1]:
 print i
>>>
美
皆
塵
凡

for...else... 語 法

NP

chart_demo = u'凡塵皆美,念念不忘'
find = False
for i in chart_demo:
 if u',' == i:
 find = True
 break
if find:
 print '找到逗號'
>>> 找到逗號

P

chart_demo = u'凡塵皆美,念念不忘'
for i in chart_demo:
 if u',' == i:
 print '找到逗號'
 break
else:
 pass 
>>> 找到逗號

lambda 函 數

NP

chart_arry = [u'凡', u'塵', u'皆', u'美', u',', u'念', u'念', u'不', u'忘']
def count_bigger_five(arry):
 return 'Yes' if len(arry) > 5 else 'No'
>>> print count_bigger_five(chart_arry)
>>> Yes

P

chart_arry = [u'凡', u'塵', u'皆', u'美', u',', u'念', u'念', u'不', u'忘']
count_bigger_five2 = lambda x: 'Yes' if len(x) > 5 else 'No'
# 這裡的x代表函式的引數,後面的式子直接是返回的結果
>>> print count_bigger_five2(chart_arry)
>>> Yes

map 函 數

NP

chart_arry = [u'凡', u'塵', u'皆', u'美']
chart_new = [x + u'+' for x in chart_arry]
>>> print chart_new
>>> ["凡+", "塵+", "皆+", "美+"]

P

chart_arry = [u'凡', u'塵', u'皆', u'美']
chart_new = map(lambda x:x + u'+', chart_arry)
>>> print chart_new
>>> ["凡+", "塵+", "皆+", "美+"]

filter 函 數

NP

chart_arry = [u'凡', u'塵', u'皆', u'美', u',', u'念', u'念', u'不', u'忘']
new_chart = []
for i in chart_arry:
 if i==u'念':
 new_chart.append(i)
>>> print new_chart
>>> ["念", "念"]

P

chart_arry = [u'凡', u'塵', u'皆', u'美', u',', u'念', u'念', u'不', u'忘']
new_chart = filter(lambda x: x if x==u'念' else None, chart_arry)
>>> print new_chart
>>> ["念", "念"]

reduce 函 數

NP

chart_arry = [u'凡', u'塵', u'皆', u'美', u',', u'念', u'念', u'不', u'忘']
chart_new = u'美句:'
for i in chart_arry:
 chart_new += i
>>> print s
>>> 美句:凡塵皆美,念念不忘

P

chart_arry = [u'凡', u'塵', u'皆', u'美', u',', u'念', u'念', u'不', u'忘']
chart_new = reduce(lambda x, y: x+y, chart_arry, u'美句:')
>>> print chart_new
>>> 美句:凡塵皆美,念念不忘

當然,pythonic還遠不止此,歡迎小夥伴補充,此文章不斷更新,歡迎收藏

對你有幫助的話,點贊加收藏吧。謝~