Since I started learning Python I decided to maintain a list of frequently used "tips". Whenever I see a piece of code that makes me think "Cool, this works!" (in an example, on StackOverflow, in open source software, etc.), I try it until I understand it, and then Add it to the list. This post is part of a cleaned up list. If you are an experienced Python programmer, although you may already know some, you may still discover some you don't know. If you are a C, C++, or Java programmer who is learning Python, or just starting to learn programming, then you will find many of them very useful like I did.
Each trick or language feature can only be verified through examples without excessive explanation. While I've tried to make the examples clear, some of them will still look a little complicated, depending on your familiarity. So if you're not sure after looking at the example, the title can provide enough information for you to get the detailed content through Google.
The list is sorted by difficulty, with commonly used language features and techniques at the front.
1.1 Split
>>> 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 Exchange Variable split
>>> a, b = 1, 2
>>> a, b = b, a
>> ;> a, b
(2, 1)
1.3 Expansion and splitting (applicable under Python 3)
>>> a, *b, c = [1, 2, 3, 4, 5]
>>> a
1
>>> b
[2, 3, 4]
>>> c
5
1.4 Negative index
>>> a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> a[-1]
10
>>> a[-3]
8
1.5 List slicing (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 List slicing using negative index
>>> a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> a[-4:-2]
[7, 8 ]
1.7 List slice with step value (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 Negative step value list slicing
>>> 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 List slice assignment
>>> 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 Named slice (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 zipPackaging and unpacking list and multiples
> >> 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'}
The above is the detailed content of 30 must-see features and skills of the Python language (1). For more information, please follow other related articles on the PHP Chinese website!