21個有關Python的小技巧

WBOY
發布: 2016-08-08 09:29:07
原創
871 人瀏覽過

從我開始學習python的時候,我就開始自己總結一個python小技巧的集合。後來當我什麼時候在Stack Overflow

或是在某個開源軟體裡看到一段很酷程式碼的時候,我就很驚訝:原來還能這麼做! ,當時我會努力的自己嘗試這段程式碼,直到我懂了它的整體思路以後,我就把這段程式碼加到我的集合裡。這篇部落格其實就是這個集合整理後一部分的公開亮相。如果你已經是個python大牛,那麼基本上你應該知道這裡面的大多數用法了,但我想你應該也能發現一些你不知道的新技巧。而如果你之前是一個c,c++,java的程式設計師,同時在學習python,或者乾脆就是一個剛學習程式設計的新手,那麼你應該會看到很多特別有用能讓你感到驚奇的實用技巧,就像我當初一樣。

每一個技巧和語言用法都會在一個個實例中展示給大家,也不需要有其他的說明。我已經盡力把每個例子弄的通俗易懂,但是因為讀者對python的熟悉程度不同,仍然可能難免有一些晦澀的地方。所以如果這些例子本身無法讓你讀懂,至少這個例子的標題會在你後面去google搜尋的時候幫你。

整個集合大概是依照難易度排序,簡單常見的在前面,比較少見的在最後。

1.1 拆箱

>>> a, b, c = 1, 2, 3

>>> a, b, c

(1, 2, 3)

>>> a, b, c, c, c = [1, 2, 3]

>>> a, b, c

(1, 2, 3)

>>> a, b, c = (2 * i + 1 for i in range(3 ))

>>> a, b, c

(1, 3, 5)

>>> a, (b, c), d = [1, (2, 3), 4]

> >> a

1

>>> b

2

>>> c

3

>>> d

4

> 1.2 拆箱1. , 2

>>> a, b = b, a

>>> a, b

(2, 1)

1.3 擴充拆箱(只相容python3)

>>>> a *b, c = [1, 2, 3, 4, 5]

>>> a

1

>>> b

[2, 3, 4]

索引

>>> a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

>>> a[-1]

10

>>> a[-1]

10

>> [-3]

8

1.5 切割清單

>>> a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

>>> a[2 :8]

[2, 3, 4, 5, 6, 7]

1.6 負數索引切割清單

>>> a = [0, 1, 2, 3, 4, 5, 6, 7, 8 , 9, 10]

>>> a[-4:-2]

[7, 8]

1.7指定步長切割清單

>>> a = [0, 1, 2, 3, 4 , 5, 6, 7, 8, 9, 10]

>>> a[::2]

[0, 2, 4, 6, 8, 10]

>>> a[::3]

[0, 3, 6, 9]

>>> a[2:8:2]

[2, 4, 6]

1.8 負數步長切割清單

>

1.8 負數步長切割清單

>

= [01.8 負數步長切割清單

>

= [01.8> 010 , 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

>>> a[::-1]

[10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

>>> a[::-2]

[10, 8, 6, 4, 2, 0]

1.9 列表切割賦值

>> , 2, 3, 4, 5]

>>> a[2:3] = [0, 0]

>>> a

[1, 2, 0, 0, 4, 5]

> >> a[1:1] = [8, 9]

>>> a

[1, 8, 9, 2, 0, 0, 4, 5]

>>> a[1:-1 ] = []

>>> a

[1, 5]

1.10 命名列表切割方式

>>>> a = [0, 1, 2, 3, 4, 5]

>> LAST = slice(-3, None)

>>> LASTTHREE

slice(-3, None, None)

>>>> a[LASTTHREE]

[3, 4, 5]

的壓縮和解壓縮

>>> a = [1, 2, 3]

>>> b = ['a', 'b', 'c']

>>> z = zip(a, b )

>>> z

[(1, 'a'), (2, 'b'), (3, 'c')]

>>> zip(*z)

[(1, 2, 3), ('a', 'b', 'c')]

1.12 列表相鄰元素壓縮器

>>> a = [1, 2, 3, 4, 5, 6]

>>> zip(*([iter(a)] * 2))

[(1, 2), (3, 4), (5, 6)]

>>> group_adjacent = lambda a, k: zip(*([iter(a)] * k))

>>> group_adjacent(a, 3)

[(1, 2, 3), (4, 5, 6)]

>>> group_adjacent (a, 2)

[(1, 2), (3, 4), (5, 6)]

>>> group_adjacent(a, 1)🎜🎜[(1,), (2,), (3,), (4,), (5,), (6,)]🎜

>>> zip(a[::2], a[1::2])

[(1, 2), (3, 4), (5, 6)]

>>> zip(a [::3], a[1::3], a[2::3])

[(1, 2, 3), (4, 5, 6)]

>>> group_adjacent = lambda a , k: zip(*(a[i::k] for i in range(k)))

>>> group_adjacent(a, 3)

[(1, 2, 3), (4, 5, 6)]

>>> group_adjacent(a, 2)

[(1, 2), (3, 4), (5, 6)]

>>> group_adjacent(a, 1)

[(>>> group_adjacent(a, 1)

[(

[( 1,), (2,), (3,), (4,), (5,), (6,)]

1.13 在列表中用壓縮器和迭代器滑動取值視窗

>>> def n_grams(a, n):

...     z = [iter(a[i:]) for i in range(n)]

...     return zip(*z)

...     return zip(*z)

... >>> a = [1, 2, 3, 4, 5, 6]

>>> n_grams(a, 3)

[(1, 2, 3), (2, 3, 4), (3 , 4, 5), (4, 5, 6)]

>>> n_grams(a, 2)

[(1, 2), (2, 3), (3, 4), (4, 5 ), (5, 6)]

>>> n_grams(a, 4)

[(1, 2, 3, 4), (2, 3, 4, 5), (3, 4, 5, 6 )]

1.14 用壓縮器反轉字典

>>> m = {'a': 1, 'b': 2, 'c': 3, 'd': 4}

>>> m. items()

[('a', 1), ('c', 3), ('b', 2), ('d', 4)]

>>> zip(m.values() , m.keys())

[(1, 'a'), (3, 'c'), (2, 'b'), (4, 'd')]

>>> mi = dict (zip(m.values(), m.keys()))

>>> mi

{1: 'a', 2: 'b', 3: 'c', 4: 'd'}

1.15 列表展開

>>> a = [[1, 2], [3, 4], [5, 6]]

>>>> list(itertools.chain.from_iterable(a))

[111 , 2, 3, 4, 5, 6]

>>> sum(a, [])

[1, 2, 3, 4, 5, 6]

>>> [x for l in a for for x in l]

[1, 2, 3, 4, 5, 6]

>>> a = [[[1, 2], [3, 4]], [[5, 6], [7 , 8]]]

>>> [x for l1 in a for l2 in l1 for x in l2]

[1, 2, 3, 4, 5, 6, 7, 8]

>>> a = [1, 2, [3, 4], [[5, 6], [7, 8]]]

>>> flatten = lambda x: [y for l in x for y in flatten(l)] if type(x) is list else [x]

>>> flatten(a)

[1, 2, 3, 4, 5, 6, 7, 8]

1.16 生成器表達式

> > g = (x ** 2 for x in xrange(10))

>>> next(g)

0

>>> next(g)

1

>>> next(g)

1

>>> next(g)

4

>>> next(g)

9

>>> sum(x ** 3 for x in xrange(10))

2025

>>> sum(x ** 3 for x1 xrange(10) if x % 3 == 1)

408

1.17 字典推導

>>> m = {x: x ** 2 for x in range(5)}

> m = {x: x ** 2 for x in range(5)}

>>> m {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}

>>> m = {x: 'A' + str(x) for x in range(10)}

> >> m

{0: 'A0', 1: 'A1', 2: 'A2', 3: 'A3', 4: 'A4', 5: 'A5', 6: 'A6', 7: 'A7', 8: 'A8', 9: 'A9'}

1.18 用字典推導反轉字典

>>> m = {'a': 1, 'b': 2, 'c': 3 , 'd': 4}

>>> m

{'d': 4, 'a': 1, 'b': 2, 'c': 3}

>>> {v: k for k, v in m.items()}

{1: 'a', 2: 'b', 3: 'c', 4: 'd'}

1.19 命名元組

>>> Point = collections.namedtuple('Point', ['x', 'y'])

>>> p = Point(x=1.0, y=2.0)

>>> p

Point(x=1.0, y =2.0)

>>> p.x

1.0

>>> p.x

1.0

>>> p.y

2.0

1.20 繼承命名元群組

>>>> class Point(collections.Point' y'])):

...     __slots__ = ()

...     def __add__(self, other):

...  y + other.y)

...

>>> p = Point(x=1.0, y=2.0)

>>> q = Point(x=2.0, y=3.0)

>> q = Point(x=2.0, y=3.0)

>>>>> p + q

Point(x=3.0, y=5.0)

1.21 操作集合

>>> A = {1, 2, 3, 3}

>>> A

set([1, 32 , 3])

>>> B = {3, 4, 5, 6, 7}

>>> B

set([3, 4, 5, 6, 7])

🎜>>>> A | B🎜🎜set([1, 2, 3, 4, 5, 6,🎜🎜免費領取LAMP兄弟連原創PHP影片教學光碟/《細說PHP》精要版,詳情諮詢官網客服:http://www .lampbrother.net🎜🎜PHPCMS二次開發http://yun.itxdl.cn/online/phpcms/index.php?u=5🎜🎜微信開發      http://yun.itxdl.cn/online/weixin/index     http://yun.itxdl.cn/online/weixin/index .php?u=5🎜🎜行動互聯網伺服器端開發http://yun.itxdl.cn/online/server/index.php?u=5🎜

Javascript課程    http://yun.itxdl.cn/online/js/index.php?u=5

CTO訓練營         http://yun.itxdl.cn/online/cto/index.php?u=5.php?

以上就介紹了21個有關Python的小技巧,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板