Each trick or language feature can only be verified by an instance, without too much explanation. Although I have tried my best to make the examples clear, some of them still seem complicated, depending on your familiarity. So if you still don't know the example, I decided to maintain a frequently used "tip" list from the beginning when I learned Python. Whenever I see a piece that makes me feel "cool, that's okay !" (In an example, in StackOverflow, in open source software, etc.), I will try it until I understand it and then add it to the list. This article is part of the list. If you are an experienced Python programmer, although you may already know something, you can still find something you don't know. If you are a C, C ++, or Java programmer learning Python, or you are learning programming at the beginning, you will find many of them very useful like me.
Each trick or language feature can only be verified by an instance, without too much explanation. Although I have tried my best to make the examples clear, some of them still seem complicated, depending on your familiarity. Therefore, if you are not clear about the example, the title can provide enough information for you to obtain detailed content through Google.
The list is sorted by difficulty. Common language features and skills are placed at the beginning.
1.30 max and min elements (heapq. nlargest and heapq. nsmallest)
>>> A = [random. randint (0,100) for _ in xrange (100)]
>>> Heapq. nsmallest (5,)
[3, 3, 5, 6, 8]
>>> Heapq. nlargest (5,)
[100,100, 99, 98, 98]
1.31 Cartesian product (itertools. product)
>>> For p in itertools. product ([1, 2, 3], [4, 5]):
(1, 4)
(1, 5)
(2, 4)
(2, 5)
(3, 4)
(3, 5)
>>> For p in itertools. product ([0, 1], repeat = 4 ):
... Print ''. join (str (x) for x in p)
...
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
1.32 combination and replacement (itertools. combinations and itertools. combinations_with_replacement)
>>> For c in itertools. combinations ([1, 2, 3, 4, 5], 3 ):
... Print ''. join (str (x) for x in c)
...
123
124
125
134
135
145
234
235
245
345
>>> For c in itertools. combinations_with_replacement ([1, 2, 3], 2 ):
... Print ''. join (str (x) for x in c)
...
11
12
13
22
23
33
1.33 sorting (itertools. permutations)
>>> For p in itertools. permutations ([1, 2, 3, 4]):
... Print ''. join (str (x) for x in p)
...
1234
1243
1324
1342
1423
1432
2134
2143
2314
2341
2413
2431
3124
3142
3214
3241
3412
3421
4123
4132
4213
4231
4312
4321
1.34 link iteration (itertools. chain)
>>> A = [1, 2, 3, 4]
>>> For p in itertools. chain (itertools. combinations (a, 2), itertools. combinations (a, 3 )):
... Print p
...
(1, 2)
(1, 3)
(1, 4)
(2, 3)
(2, 4)
(3, 4)
(1, 2, 3)
(1, 2, 4)
(1, 3, 4)
(2, 3, 4)
>>> For subset in itertools. chain. from_iterable (itertools. combinations (a, n) for n in range (len (a) + 1 ))
... Print subset
...
()
(1 ,)
(2 ,)
(3 ,)
(4 ,)
(1, 2)
(1, 3)
(1, 4)
(2, 3)
(2, 4)
(3, 4)
(1, 2, 3)
(1, 2, 4)
(1, 3, 4)
(2, 3, 4)
(1, 2, 3, 4)
1.35 group rows by given value (itertools. groupby)
>>> From operator import itemgetter
>>> Import itertools
>>> With open('contactlenses.csv ', 'r') as infile:
... Data = [line. strip (). split (',') for line in infile]
...
>>> Data = data [1:]
>>> Def print_data (rows ):
... Print '\ n'. join (' \ t'. join ('{:< 16}'. format (s) for s in row) for row in rows)
...
>>> Print_data (data)
Young myope no supported CED none
Young myope no normal soft
Young myope yes reduced none
Young myope yes normal hard
Young hypermetrope no supported CED none
Young hypermetrope no normal soft
Young hypermetrope yes reduced none
Young hypermetrope yes normal hard
Pre-presbyopic myope no supported CED none
Pre-presbyopic myope no normal soft
Pre-presbyopic myope yes already CED none
Pre-presbyopic myope yes normal hard
Pre-presbyopic hypermetrope no supported CED none
Pre-presbyopic hypermetrope no normal soft
Pre-presbyopic hypermetrope yes already CED none
Pre-presbyopic hypermetrope yes normal none
Presbyopic myope no supported CED none
Presbyopic myope no normal none
Presbyopic myope yes reduced none
Presbyopic myope yes normal hard
Presbyopic hypermetrope no supported CED none
Presbyopic hypermetrope no normal soft
Presbyopic hypermetrope yes reduced none
Presbyopic hypermetrope yes normal none
>>> Data. sort (key = itemgetter (-1 ))
>>> For value, group in itertools. groupby (data, lambda r: r [-1]):
... Print '-----------'
... Print 'group: '+ value
... Print_data (group)
...
-----------
Group: hard
Young myope yes normal hard
Young hypermetrope yes normal hard
Pre-presbyopic myope yes normal hard
Presbyopic myope yes normal hard
-----------
Group: none
Young myope no supported CED none
Young myope yes reduced none
Young hypermetrope no supported CED none
Young hypermetrope yes reduced none
Pre-presbyopic myope no supported CED none
Pre-presbyopic myope yes already CED none
Pre-presbyopic hypermetrope no supported CED none
Pre-presbyopic hypermetrope yes already CED none
Pre-presbyopic hypermetrope yes normal none
Presbyopic myope no supported CED none
Presbyopic myope no normal none
Presbyopic myope yes reduced none
Presbyopic hypermetrope no supported CED none
Presbyopic hypermetrope yes reduced none
Presbyopic hypermetrope yes normal none
-----------
Group: soft
Young myope no normal soft
Young hypermetrope no normal soft
Pre-presbyopic myope no normal soft
Pre-presbyopic hypermetrope no normal soft
Presbyopic hypermetrope no normal soft