從我開始學習Python時我就決定要維護一個經常使用的「訣竅」清單。不論何時當我看到一段讓我覺得「酷,這樣也行!」的程式碼(在一個例子中、在StackOverflow、在開源軟體中,等等),我會嘗試它直到理解它,然後把它加到列表中。這篇文章是清理過清單的一部分。如果你是個有經驗的Python程式設計師,儘管你可能已經知道一些,但你仍能發現一些你不知道的。如果你是一個正在學習Python的C、C++或Java程式設計師,或是剛開始學習編程,那麼你會像我一樣發現它們中的許多非常有用。
每個訣竅或語言特性只能透過實例來驗證,無需過多解釋。雖然我已盡力使例子清晰,但它們中的一些仍會看起來有些複雜,這取決於你的熟悉程度。所以如果看過例子後還不清楚的話,標題能夠提供足夠的資訊讓你透過Google獲取詳細的內容。
清單依難度排序,常用的語言特徵和技巧放在前面。
1.1 分拆
>>> a, b, c = 1, 2, 3
>>> a, b, c
(1, 2, 3)
>>> a, b, c = [1, 2, 3]
>>> a , b, c
(1, 2, 3)
(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 交換變數分拆
>>> a, b = 1, 2
>>> a, b = b, a
#>> ;> a, b
(2, 1)
1.3 拓展分拆(Python 3下適用)
>>> a, *b, c = [1, 2, 3, 4, 5]
>>> a
1
>>> b
[2, 3, 4]
>>> c
5
1.4 負指數
>>> a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> a[-1]
#10
1.5 清單切片(a[start:
end#])
>>> 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[start:end:step])
>>> 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 負步進值得列表切片
>>> a = [0, 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 列表切片賦值
>>> a = [1, 2, 3, 4, 5]
>>> a[>> 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 命名切片(slice(start, end, step))
#>>> a = [0, 1, 2, 3, 4, 5]
>>> LASTTHREE = slice(-3, None)
#>>> LASTTHREE
slice(-3, None, None)
#>##slice(-3, None, None) #> ;>> a[LASTTHREE]
[3, 4, 5]
#1.11
zip打包解包列表與倍數
#> >> 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 使用zip合併後續的清單項目
>>>> 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)
[(1,), (2,), (3,), (4,), (5,), (6,)]
1.13使用zip和iterators產生滑動視窗(n -grams)
>>>> from itertools import islice
>>>> def n_grams(a, n):
... z = (islice(a, i, None) for i in range(n))
#... 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使用zip字典
>> 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'}
##
以上是必看的30個Python語言的特點技巧(1)的詳細內容。更多資訊請關注PHP中文網其他相關文章!