From the time I started learning python, I summed up a set of python to start their own small tips. Later, when I when in Stack Overflow or see some really cool code for an open source software in time, I am very surprised: the original can do! , Then I will work hard to try themselves at the code until I understand it's the whole idea, I put this code added to my collection. This blog is actually a public appearance after Nike Free Run Womens finishing this part of the collection. If you are already a python Daniel, then basically you should know that most use this inside, but I think you should be able to find some new tricks that you do not know. And if you are 2015 Nike Free 5.0 a previous c, c ++, java programmers, while learning python, or simply a novice just learning to program, then you should see a lot of make you surprised particularly useful practical tips, like I did the same. Each skills and language usage will show you in one instance, we do not need Nike Free TR Fit any other explanation. I've tried to get in each case straightaway, but because of the different degrees of familiarity with python, and still may inevitably there are some obscure place. So if these examples alone can not make you understand, at least in this example the title behind you time to google search will help you. The entire collection is probably sorted by degree of difficulty, in front of a simple common, relatively rare in the final. 1.1 Unpacking \u0026 gt; \u0026 gt; \u0026 gt; a, b, c = 1, 2, 3 \u0026 gt; \u0026 gt; \u0026 gt; a, b, c (1, 2, 3) \u0026 gt; \u0026 gt; \u0026 gt; a, b, c = [ 1, 2, 3] \u0026 gt; \u0026 gt; \u0026 gt; a, b, c (1, 2, 3) \u0026 gt; \u0026 gt; \u0026 gt; a, b, c = (2 * i + 1 for i in range (3)) \u0026 gt; \u0026 gt; \u0026 gt; a, b, c (1, 3, 5) \u0026 gt; \u0026 gt; \u0026 gt; a, (b, c), d = [1, (2, 3), 4] \u0026 gt; \u0026 gt; \u0026 gt ; a1 \u0026 gt; \u0026 gt; \u0026 gt; b2 \u0026 gt; \u0026 gt; \u0026 gt; c3 \u0026 gt; \u0026 gt; \u0026 gt; d41.2 unpacking variable exchange \u0026 gt; \u0026 gt; \u0026 gt; a, b = 1, 2 \u0026 gt; \u0026 gt; \u0026 gt; a, b = b, a \u0026 gt; \u0026 gt; \u0026 gt; a, b (2, 1) 1.3 Extended unpacking (only compatible python3) \u0026 gt; \u0026 gt; \u0026 gt; a, * b, c = [1, 2, 3, 4, 5] \u0026 gt; \u0026 gt ; \u0026 gt; a1 \u0026 gt; \u0026 gt; \u0026 gt; b [2, 3, 4] \u0026 gt; \u0026 gt; \u0026 gt; c51.4 negative index \u0026 gt; \u0026 gt; \u0026 gt; a = [0, 1, 2, 3, 4, 5, 6 , 7, 8, 9, 10] \u0026 gt; \u0026 gt; \u0026 gt; a [-1] 10 \u0026 gt; \u0026 gt; \u0026 gt; a [-3] 81.5 cutting list \u0026 gt; \u0026 gt; \u0026 gt; a = [0, 1, 2, 3 , 4, 5, 6, 7, 8, 9, 10] \u0026 gt; \u0026 gt; \u0026 gt; a [2: 8] [2, 3, 4, 5, 6, 7] 1.6 negative index cutting list \u0026 gt; \u0026 gt; \u0026 gt ; a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] \u0026 gt; \u0026 gt; \u0026 gt; a [-4: -2] [7, 8] 1.7 specifies the steps cut list \u0026 gt; \u0026 gt; \u0026 gt; a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] \u0026 gt; \u0026 gt; \u0026 gt; a [:: 2] [0, 2, 4 , 6, 8, 10] \u0026 gt; \u0026 gt; \u0026 gt; a [:: 3] [0, 3, 6, 9] \u0026 gt; \u0026 gt; \u0026 gt; a [2: 8: 2] [2, 4, 6] 1.8 Negative step cut list \u0026 gt; \u0026 gt; \u0026 gt; a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] \u0026 gt; \u0026 gt; \u0026 gt; a [:: - 1] [ 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0] \u0026 gt; \u0026 gt; \u0026 gt; a [:: - 2] [10, 8, 6, 4, 2, 0] 1.9 listing Cutting Assignment \u0026 gt; \u0026 gt; \u0026 gt; a = [1, 2, 3, 4, 5] \u0026 gt; \u0026 gt; \u0026 gt; a [2: 3] = [0, 0] \u0026 gt; \u0026 gt; \u0026 gt; a [1, 2 , 0, 0, 4, 5] \u0026 gt; \u0026 gt; \u0026 gt; a [1: 1] = [8, 9] \u0026 gt; \u0026 gt; \u0026 gt; a [1, 8, 9, 2, 0, 0, 4, 5 ] \u0026 gt; \u0026 gt; \u0026 gt; a [1: -1] = [] \u0026 gt; \u0026 gt; \u0026 gt; a [1, 5] 1.10 naming list cut mode \u0026 gt; Air Max 2011 Womens Grey Green Black \u0026 gt; \u0026 gt; a = [0, 1, 2, 3 , 4, 5] \u0026 gt; \u0026 gt; \u0026 gt; LASTTHREE = slice (-3, None) \u0026 gt; \u0026 gt; \u0026 gt; LASTTHREEslice (-3, None, None) \u0026 gt; \u0026 gt; \u0026 gt; a [LASTTHREE] [3, 4, 5] 1.11 lists and iterators compression and decompression \u0026 gt; \u0026 gt; \u0026 gt; a = [1, 2, 3] \u0026 gt; \u0026 gt; \u0026 gt; b = ['a', 'b', 'c'] \u0026 gt; \u0026 gt ; \u0026 gt; z = zip (a, b) \u0026 gt; \u0026 gt; \u0026 gt; z [(1, 'a'), (2, 'b'), (3, 'c')] \u0026 gt; \u0026 gt; \u0026 gt; zip (* z) [(1, 2, 3), ('a', 'b', 'c')] 1.12 list of neighbors Compressors \u0026 gt; \u0026 gt; \u0026 gt; a = [1, 2, 3, 4 , 5, 6] \u0026 gt; \u0026 gt; \u0026 gt; zip (* ([iter (a)] * 2)) [(1, 2), (3, 4), (5, 6)] \u0026 gt; \u0026 gt; \u0026 gt; group_adjacent = lambda a, k: zip (* ([iter (a)] * k)) \u0026 gt; \u0026 gt; \u0026 gt; group_adjacent (a, 3) [(1, 2, 3), (4, 5, 6)] \u0026 gt; \u0026 gt; \u0026 gt; group_adjacent (a, 2) [(1, 2), (3, 4), (5, 6)] \u0026 gt; \u0026 gt; \u0026 gt; group_adjacent (a, 1) [(1,), ( 2,), (3,), (4,), (5,), (6,)] \u0026 gt; \u0026 gt; \u0026 gt; zip (a [:: 2], a [1 :: 2]) [(1 , 2), (3, 4), (5, 6)] \u0026 gt; \u0026 gt; \u0026 gt; zip (a [:: 3], a [1 :: 3], a [2 :: 3]) [(1 , 2, 3), (4, 5, 6)] \u0026 gt; \u0026 gt; \u0026 gt; group_adjacent = lambda a, k: zip (* (a [i :: k] for i in range (k))) \u0026 gt; \u0026 gt ; \u0026 gt; group_adjacent (a, 3) [(1, 2, 3), (4, 5, 6)] \u0026 gt; \u0026 gt; \u0026 gt; group_adjacent (a, 2) [(1, 2), (3, 4) , (5, 6)] \u0026 gt; \u0026 gt; \u0026 gt; group_adjacent (a, 1) [(1,), (2,), (3,), (4,), (5,), (6,)] 1.13 Compression and iterators in the list by sliding value window \u0026 gt; \u0026 gt; \u0026 gt; def n_grams (a, n): ... z = [iter (a [i:]) for i in range (n)] ... return zip (* z) ... \u0026 gt; \u0026 gt; \u0026 gt; a = [1, 2, 3, 4, 5, 6] \u0026 gt; \u0026 gt; \u0026 gt; n_grams (a, 3) [(1, 2 , 3), (2, 3, 4), (3, 4, 5), (4, 5, 6)] \u0026 gt; \u0026 gt; \u0026 gt; n_grams (a, 2) [(1, 2), (2, 3), (3, 4), (4, 5), (5, 6)] \u0026 gt; \u0026 gt; \u0026 gt; n_grams (a, 4) [(1, 2, 3, 4), (2, 3, 4 , 5), (3, 4, 5, 6)] 1.14 compressed inverting dictionary \u0026 gt; \u0026 gt; \u0026 gt; m = {'a': 1, 'b': 2, 'c': 3, 'd ': 4} \u0026 gt; \u0026 gt; \u0026 gt; m.items () [(' a ', 1), (' c ', 3), (' b ', 2), (' d ', 4)] \u0026 gt; \u0026 gt; \u0026 gt; zip (m.values (), m.keys ()) [(1, 'a'), (3, 'c'), (2, 'b'), (4, 'd') ] \u0026 gt; \u0026 gt; \u0026 gt; mi = dict (zip (m.values (), m.keys ())) \u0026 gt; \u0026 gt; \u0026 gt; mi {1: 'a', 2: 'b', 3: 'c ', 4:' d '} 1.15 List Expansion \u0026 gt; \u0026 gt; \u0026 gt; a = [[1, 2], [3, 4], [5, 6]] \u0026 gt; \u0026 gt; \u0026 gt; list (itertools.chain. from_iterable (a)) [1, 2, 3, 4, 5, 6] \u0026 gt; \u0026 gt; \u0026 gt; sum (a, []) [1, 2, 3, 4, 5, 6] \u0026 gt; \u0026 gt; \u0026 gt; [x for l in a for x in Nike Free Womens l] [1, 2, 3, 4, 5, 6] \u0026 gt; \u0026 gt; \u0026 gt; a = [[[1, 2], [3, 4]], [[5 , 6], [7, 8]]] \u0026 gt; \u0026 gt; \u0026 gt; [x for l1 in a for l2 in l1 for x in l2] [1, 2, 3, 4, 5, 6, 7, 8] \u0026 gt ; \u0026 gt; \u0026 gt; a = [1, 2, [3, 4], [[5, 6], [7, 8]]] \u0026 gt; \u0026 gt; \u0026 gt; flatten = lambda x: [y for l in x for y in flatten (l)] if type (x) is list else [x] \u0026 gt; \u0026 gt; \u0026 gt; flatten (a) [1, 2, 3, 4, 5, 6, 7, 8] 1.16 generator expression \u0026 gt; \u0026 gt; \u0026 gt; g = (x ** 2 for x in xrange (10)) \u0026 gt; \u0026 gt; \u0026 gt; next (g) 0 \u0026 gt; \u0026 gt; \u0026 gt; next (g) 1 \u0026 gt; \u0026 gt; \u0026 gt; next (g ) 4 \u0026 gt; \u0026 gt; Mens Nike Free 3.0 Wool Skin Shoes Blue Yellow \u0026 gt; Mens Nike Free 3.0 V2 Shoes White Black Red next (g) 9 \u0026 gt; \u0026 gt; \u0026 gt; sum (x ** 3 for x in xrange (10)) 2025 \u0026 gt; \u0026 gt; \u0026 gt; sum (x ** 3 for x in xrange (10) if x% 3 == 1) 4081.17 dictionary derived \u0026 gt; \u0026 gt; \u0026 gt; m = {x: x ** 2 for x in range (5)} \u0026 gt; \u0026 gt; \u0026 gt; m {0: 0, 1: 1, 2: 4, 3: 9, 4: 16} \u0026 gt; \u0026 gt; \u0026 gt; m = {x: 'A' + str (x) for x in range (10)} \u0026 gt; \u0026 gt; \u0026 gt; m {0: ' A0 ', 1:' A1 ', 2:' A2 ', 3:' A3 ', 4:' A4 ', 5:' A5 ', 6:' A6 ', 7:' A7 ', 8:' A8 ' , 9: 'A9'} 1.18 dictionary derived reverse dictionary \u0026 gt; \u0026 gt; \u0026 gt; m = {'a': 1, 'b': 2, 'c': 3, 'd': 4} \u0026 gt; \u0026 gt ; \u0026 gt; m {'d': 4, 'a': 1, 'b': 2, 'c': 3} \u0026 gt; \u0026 gt; \u0026 gt; {v: k for k, v in m.items ()} {1: 'a', 2: 'b', 3: 'c', 4: 'd'} 1.19 named tuple \u0026 gt; \u0026 gt; \u0026 gt; Point = collections.namedtuple ('Point', ['x', 'y']) \u0026 gt; \u0026 gt; \u0026 gt; p = Point (x = 1.0, y = 2.0) \u0026 gt; \u0026 gt; \u0026 gt; pPoint (x = 1.0, y = 2.0) \u0026 gt; \u0026 gt; \u0026 gt; p.x1.0 \u0026 gt ; \u0026 gt; \u0026 gt; p.y2.01.20 inherit named tuple \u0026 gt; \u0026 gt; \u0026 gt; class Point (collections.namedtuple ('PointBase', ['x', 'y'])): ... __slots__ = () ... def __add __ (self, other): ... return Point (x = self.x + other.x, y = self.y + other.y) ... \u0026 gt; \u0026 gt; \u0026 gt; p = Point ( x = 1.0, y = 2.0) \u0026 gt; \u0026 gt; \u0026 gt; q = Point (x = 2.0, y = 3.0) \u0026 gt; \u0026 gt; \u0026 gt; p + qPoint (x = 3.0, y = 5.0) 1.21 set of operations \u0026 gt; \u0026 gt ; \u0026 gt; A = {1, 2, 3, 3} \u0026 gt; \u0026 gt; \u0026 gt; Aset ([1, 2, 3]) \u0026 gt; \u0026 gt; \u0026 gt; B = {3, 4, 5, 6, 7} \u0026 gt ; \u0026 gt; \u0026 gt; Bset ([3, 4, 5, 6, 7]) \u0026 gt; \u0026 gt; \u0026 gt; A | Bset ([1, 2, 3, 4, 5, 6, 7]) \u0026 gt; \u0026 gt; \u0026 gt ; A \u0026 amp; Bset ([3]) \u0026 gt; \u0026 gt; \u0026 gt; A - Bset ([1, 2]) \u0026 gt; \u0026 gt; \u0026 gt; B - Aset ([4, 5, 6, 7]) \u0026 gt; \u0026 gt; \u0026 gt; A ^ Bset ([1, 2, 4, 5, 6, 7]) \u0026 gt; \u0026 gt; \u0026 gt; (A ^ B) == ((A - B) | (B - A)) True1.22 Operation Multiple collections \u0026 gt; \u0026 gt; \u0026 gt; A = collections.Counter ([1, 2, 2]) \u0026 gt; \u0026 gt; \u0026 gt; B = collections.Counter ([2, 2, 3]) \u0026 gt; \u0026 gt; \u0026 gt; ACounter ( {2: 2, 1: 1}) \u0026 gt; \u0026 gt; \u0026 gt; BCounter ({2: 2, 3: 1}) \u0026 gt; \u0026 gt; \u0026 gt; A | BCounter ({2: 2, 1: 1, 3: 1 }) \u0026 gt; \u0026 gt; \u0026 gt; A \u0026 amp; BCounter ({2: 2}) \u0026 gt; \u0026 gt; \u0026 gt; A + BCounter ({2: 4, 1: 1, 3: 1}) \u0026 gt; \u0026 gt; \u0026 gt; A - BCounter ({1: 1}) \u0026 gt; \u0026 gt; \u0026 gt; B - ACounter ({3: 1}) 1.23 statistical elements may iterator most commonly occurring \u0026 gt; \u0026 gt; \u0026 gt; A = collections.Counter ([ 1, 1, 2, 2, 3, 3, 3, 3, 4, 5, 6, 7]) \u0026 gt; \u0026 gt; \u0026 gt; ACounter ({3: 4, 1: 2, 2: 2, 4: 1, 5: 1, 6: 1, 7: 1}) \u0026 gt; \u0026 gt; \u0026 gt; A.most_common (1) [(3, 4)] \u0026 gt; \u0026 gt; Air Jordan Outlet \u0026 gt; A.most_common (3) [(3, 4) , (1, 2), (2, 2)] 1.24 both ends can be operated queue \u0026 gt; \u0026 gt; \u0026 gt; Q = collections.deque () \u0026 gt; \u0026 gt; \u0026 gt; Q.append (1) \u0026 gt; \u0026 gt; \u0026 gt; Q.appendleft (2) \u0026 gt; \u0026 gt; \u0026 gt; Q.extend ([3, 4]) \u0026 gt; \u0026 gt; \u0026 gt; Q.extendleft ([5, 6]) \u0026 gt; \u0026 gt; \u0026 gt; Qdeque ([6 , 5, 2, 1, 3, 4]) \u0026 gt; \u0026 gt; \u0026 gt; Q.pop () 4 \u0026 gt; \u0026 gt; \u0026 gt; Q.popleft () 6 \u0026 gt; \u0026 gt; \u0026 gt; Qdeque ([5, 2, 1, 3 ]) \u0026 gt; \u0026 gt; \u0026 gt; Q.rotate (3) \u0026 gt; \u0026 gt; 535807 103 Jordan CP3.VI White Black Game Royal Sport Red CP3 Shoes 2013 Sale \u0026 gt; Qdeque ([2, 1, 3, 5]) \u0026 gt; \u0026 gt; \u0026 gt; Q.rotate (-3) \u0026 gt; \u0026 gt; \u0026 gt ; Qdeque ([5, 2, 1, 3]) 1.25 maximum length deque \u0026 gt; \u0026 gt; \u0026 gt; last_three = collections.deque (maxlen = 3) \u0026 gt; \u0026 gt; \u0026 gt; for i in xrange (10) : ... last_three.append (i) ... print ',' .join (str (x) for x in last_three) ... 00, 10, 1, 21, 2, 32, 3, 43, 4, 54, 5, 65, 6, 76, 7, 87, 8, 91.26 sortable dictionary \u0026 gt; \u0026 gt; \u0026 gt; m = Air Max 2011 Men Blue Black dict ((str (x), x) for x in range (10)) \u0026 gt; \u0026 gt; \u0026 gt; print ',' .join (m.keys ()) 1, 0, 3, 2, 5, Nike Air Max 95 4, 7, 6, 9, 8 \u0026 gt; \u0026 gt; \u0026 gt; m = collections.OrderedDict ((str (x) , x) for x in range (10)) \u0026 gt; \u0026 gt; \u0026 gt; print ',' .join (m.keys ()) 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 Air Max 2011 Womens Blue \u0026 gt; \u0026 gt; \u0026 gt; m = collections.OrderedDict ((str (x), x) for x in range (10, 0, -1)) \u0026 gt; \u0026 gt; \u0026 gt; print ',' .join (m.keys ()) 10, 9, 8, 7, 6, 5, 4, 3, 2, 11.27 default dictionary \u0026 gt; \u0026 gt; \u0026 gt; m = dict () \u0026 gt; \u0026 gt; \u0026 gt; m ['a'] Traceback (most recent call last ): File \u0026 quot; \u0026 lt; stdin \u0026 gt; \u0026 quot ;, line 1, in \u0026 lt; module \u0026 gt; KeyError: 'a' \u0026 gt; \u0026 gt; \u0026 gt; \u0026 gt; \u0026 gt; \u0026 gt; m = collections.defaultdict (int) \u0026 gt; \u0026 gt; \u0026 gt ; m ['a'] 0 \u0026 gt; \u0026 gt; \u0026 gt; m ['b'] 0 \u0026 gt; \u0026 gt; \u0026 gt; m = collections.defaultdict (str) \u0026 gt; \u0026 gt; \u0026 gt; m ['a'] '' \u0026 gt; \u0026 gt ; \u0026 gt; m ['b'] + = 'a' \u0026 gt; \u0026 gt; \u0026 gt; m ['b'] 'a' \u0026 gt; \u0026 gt; \u0026 gt; m = collections.defaultdict (lambda: '[default value]') \u0026 gt; \u0026 gt; \u0026 gt; m ['a'] '[default value]' \u0026 gt; \u0026 gt; \u0026 gt; m ['b'] '[default value]' 1.28 default dictionary of simple tree-expression \u0026 gt; \u0026 gt; \u0026 gt; import json \u0026 gt; \u0026 gt; \u0026 gt; tree = lambda: collections.defaultdict (tree) \u0026 gt; \u0026 gt; \u0026 gt; root = tree () \u0026 gt; \u0026 gt; \u0026 gt; root ['menu'] ['id'] = 'file' \u0026 gt ; \u0026 gt; \u0026 gt; root ['menu'] ['value'] = 'File' \u0026 gt; \u0026 gt; \u0026 gt; root ['menu'] ['menuitems'] [' new '] [' value '] =' New '\u0026 gt; \u0026 gt; \u0026 gt; root [' menu '] [' menuitems '] [' new '] [' onclick '] =' new (); '\u0026 gt; \u0026 gt; \u0026 gt; root [' menu '] [' menuitems '] [' open '] [' value '] =' Open '\u0026 gt; \u0026 gt; \u0026 gt; root [' menu '] [' menuitems'] ['open'] ['onclick'] = 'open ();' \u0026 gt; \u0026 gt; \u0026 gt; root ['menu'] ['menuitems'] [' close '] [' value '] =' Close '\u0026 gt; \u0026 gt; \u0026 gt; root [' menu '] [' menuitems'] [' close '] [' onclick '] =' close (); '\u0026 gt; \u0026 gt; \u0026 gt; print json.dumps (root, sort_keys = True, indent = 4, separators = (', ',': ')) {\u0026 quot ; menu \u0026 quot ;: {\u0026 quot; id \u0026 quot ;: \u0026 quot; file \u0026 quot ;, \u0026 quot; menuitems \u0026 quot ;: {\u0026 quot; close \u0026 quot ;: {\u0026 quot; onclick \u0026 quot ;: \u0026 quot; close (); \u0026 quot ;, \u0026 quot; value \u0026 quot ;: \u0026 quot; Close \u0026 quot; }, \u0026 quot; new \u0026 quot ;: {\u0026 quot; onclick \u0026 quot ;: \u0026 quot; new (); \u0026 quot ;, \u0026 quot; value \u0026 quot ;: \u0026 quot; New \u0026 quot;}, \u0026 quot; open \u0026 quot ;: {\u0026 quot; onclick \u0026 quot ;: \u0026 quot; open (); \u0026 quot ;, \u0026 quot; value \u0026 quot ;: \u0026 quot; Open \u0026 quot;}}, \u0026 quot; value \u0026 quot ;: \u0026 quot; File \u0026 quot;}} 1.29 object to the mapping of the sole count of \u0026 gt; \u0026 gt; \u0026 gt; import itertools, collections \u0026 gt; \u0026 gt; \u0026 gt; value_to_numeric_map = collections.defaultdict (. itertools.count () next) \u0026 gt; \u0026 gt; \u0026 gt; value_to_numeric_map ['a'] 0 \u0026 gt; \u0026 gt; \u0026 gt; value_to_numeric_map ['b'] 1 \u0026 gt; \u0026 gt; \u0026 gt; value_to_numeric_map ['c'] 2 \u0026 gt; \u0026 gt; \u0026 gt; value_to_numeric_map ['a'] 0 \u0026 gt; \u0026 gt; \u0026 gt; value_to_numeric_map ['b'] 11.30 minimum and maximum number of list elements \u0026 gt; \u0026 gt; \u0026 gt; a = [random.randint (0, 100) for __ in xrange (100)] \u0026 gt; \u0026 gt; \u0026 gt; heapq.nsmallest (5, a) [3, 3, 5, 6, 8] \u0026 gt; \u0026 gt; \u0026 gt; heapq.nlargest (5, a) [100, 100, 99, 98, 98] Cartesian product of two lists of 1.31 \u0026 gt; \u0026 gt; \u0026 gt; for p in itertools.product ([1, 2, 3], [4, 5]) :( 1, 4) (1, 5) (2, 4) (2, 5) (3, 4) (3, 5) \u0026 gt; \u0026 gt; \u0026 gt; for p in itertools.product ([0, 1], repeat = 4): ... print '' .join (str (x) for x in p) ... 00000001001000110100010101100111100010011010101111001101111011111.32 list of combinations and list elements substitute composition \u0026 gt; \u0026 gt; \u0026 gt; for c in itertools.combinations ([1, 2, 3, 4, 5], 3): ... print '' .join (str (x) for x in c) ... 123124125134135145234235245345 \u0026 gt; \u0026 gt; \u0026 gt; for c in itertools.combinations_with_replacement ([1, 2, 3], 2): .. . print '' .join (str (x) for x in c) ... 1112132223331.33 list element permutations \u0026 gt; \u0026 gt; \u0026 gt; for p in itertools.permutations ([1, 2, 3, 4]): .. . print '' .join (str (x) for x in p) ... 1234124313241342142314322134214323142341241324313124314232143241341234214123413242134231431243211.34 can Nike Air Max link iterator \u0026 gt; \u0026 gt; \u0026 gt; a = [1, 2, 3, 4] \u0026 gt; \u0026 gt; \u0026 gt; 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) \u0026 gt; \u0026 gt; \u0026 gt; 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) 2015 Nike KD 7 Kids Basketball Shoes Sport Turq Charcoal Atomic Green Outlet (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 for the specified column clustering based on the file \u0026 gt; \u0026 gt; \u0026 gt; import itertools \u0026 gt; \u0026 gt; \u0026 gt; with open ('contactlenses.csv', 'r' ) as infile: ... data = [. line.strip () split (',') for line in infile] ... \u0026 gt; \u0026 gt; \u0026 gt; data = data [1:] \u0026 gt; \u0026 gt; \u0026 gt; def print_data (rows): ... print '\\ n'.join (' \\ t'.join (. '{: \u0026 lt; 16}' format (s) for s in row) for row in rows) ... \u0026 gt ; \u0026 gt; \u0026 gt; print_data (data) young myope no reduced noneyoung myope no normal softyoung myope yes reduced noneyoung myope yes normal hardyoung hypermetrope no reduced noneyoung hypermetrope no normal softyoung hypermetrope yes reduced noneyoung hypermetrope yes normal hardpre-presbyopic myope no reduced nonepre-presbyopic myope no normal softpre-presbyopic myope yes reduced nonepre-presbyopic myope yes normal hardpre-presbyopic hypermetrope no reduced nonepre-presbyopic hypermetrope no Nike Free Womens normal softpre-presbyopic hypermetrope yes reduced nonepre-presbyopic hypermetrope yes normal nonepresbyopic myope no reduced nonepresbyopic myope no normal nonepresbyopic myope yes reduced nonepresbyopic myope yes normal hardpresbyopic hypermetrope no reduced nonepresbyopic hypermetrope no normal softpresbyopic hypermetrope yes reduced nonepresbyopic hypermetrope yes normal none \u0026 gt; \u0026 gt; \u0026 gt; data.sort (key = lambda r: r [-1]) \u0026 gt; \u0026 gt; \u0026 gt; for value , group in itertools.groupby (data, lambda r: r [-1]): ... print '-----------'... print' Group: '+ value ... print_data (group) ...----------- Group: hardyoung myope yes normal hardyoung hypermetrope yes normal hardpre-presbyopic myope yes normal hardpresbyopic myope yes normal hard ----------- Group : noneyoung myope no reduced noneyoung myope yes reduced noneyoung hypermetrope no reduced noneyoung hypermetrope yes reduced nonepre-presbyopic myope no reduced nonepre-presbyopic myope yes reduced nonepre-presbyopic hypermetrope no reduced nonepre-presbyopic hypermetrope yes reduced nonepre-presbyopic hypermetrope yes normal nonepresbyopic myope no reduced nonepresbyopic myope no normal nonepresbyopic myope yes reduced nonepresbyopic hypermetrope no reduced nonepresbyopic hypermetrope yes reduced nonepresbyopic hypermetrope yes normal none ----------- Group: softyoung myope no normal softyoung hypermetrope no normal softpre-presbyopic myope no normal softpre -presbyopic hypermetrope no normal softpresbyopic hypermetrope no normal soft30 个 about Python tips