python語法糖

反轉字符串

>>> a = "codementor"
>>> print "Reverse is",a[::-1]

矩陣轉置

>>> mat = [[1, 2, 3], [4, 5, 6]]
>>> zip(*mat)
[(1, 4), (2, 5), (3, 6)]

列表轉字符串

python語法糖

a = ["Code", "mentor", "Python", "Developer"]
>>> print " ".join(a)

列表拉鍊

>>> for x, y in zip(list1,list2):
... print x, y
...
a p
b q
c r
d s

多重嵌套列表轉單一列表

>>> import itertools 
>>> list(itertools.chain.from_iterable(a))
[1, 2, 3, 4, 5, 6]

檢查兩個單詞是否是相互顛倒的

python語法糖

from collections import Counter
def is_anagram(str1, str2):
return Counter(str1) == Counter(str2)
>>> is_anagram('abcd','dbca')
True
>>> is_anagram('abcd','dbaa')
False

一行代碼獲取用戶輸入並放到列表中

>>> result = map(lambda x:int(x) ,raw_input().split())
1 2 3 4
>>> result
[1, 2, 3, 4]

​後記:對於大部分轉行的人來說,找機會把自己的基礎知識補齊,邊工作邊補基礎知識,真心很重要。"我們相信人人都可以成為一個IT大神,現在開始,選擇一條陽光大道,助你入門,學習的路上不再迷茫。這裡是北京尚學堂,初學者轉行到IT行業的聚集地。"


分享到:


相關文章: