您好,登錄后才能下訂單哦!
小編給大家分享一下Python語言有哪些特點,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
每個竅門或語言特性只能通過實例來驗證,無需過多解釋。雖然我已盡力使例子清晰,但它們中的一些仍會看起來有些復雜,這取決于你的熟悉程度。所以如果看過例子后還不清楚的話,標題能夠提供足夠的信息讓你通過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) >>> 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 >>> a[-3] 8
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[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) >>> 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'}
以上是“Python語言有哪些特點”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。