Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ''' from Transforming Code into Beautiful, Idiomatic Python
- by Raymond Hettinger'''
- for i in [0, 1, 2, 3, 4, 5]:
- print i**2
- for i in range(6):
- print i**2
- for i in xrange(6):
- print i**2
- colors = ['red', 'green', 'blue', 'yellow']
- # don't do this
- for i in range (len(colors)):
- print colors[i]
- # do this
- for color in colors:
- print color
- # don't
- for i in range(len(colors) -1, -1, -1):
- print colors[i]
- # do
- for color in reversed(colors):
- print color
- # don't
- for i in range(len(colors)):
- print i, '-->' colors[i]
- # do
- for i, color in enumerate(colors):
- print i, '-->', colors[i]
- ''' Looping over two collections '''
- names = ['raymond', 'rachel', 'matthew']
- colors = ['red', 'green', 'blue', 'yellow']
- # don't
- n = min(len(names), len(colors))
- for i in range(n):
- print names[i], '-->', colors[i]
- # don't
- for name, color in zip(names, colors):
- print name, '-->', color
- # do
- for name, color in izip(names, colors):
- print name, '-->', color
- ''' Looping in sorted order '''
- colors = ['red', 'green', 'blue', 'yellow']
- for color in sorted(colors):
- print color
- for color in sorted(colors, reverse=True):
- print color
- def compare_length(c1, c2):
- if len(c1) < len(c2): return -1
- if len(c1) > len(c2): return 1
- return 0
- # don't
- print sorted(colors, cmp=compare_length)
- # do
- print sorted(colors, key=len)
- # Call a function until a sentinel value
- blocks = []
- while True:
- block = f.read(32)
- if block == '':
- break
- blocks.append(block)
- blocks = []
- for block in iter(partial(f.read, 32), ''):
- blocks.append(block)
- # Distinguishing mulitiple exit points in loops
- def find(seq, target):
- found = False
- for i, value in enumerate(seq):
- if value == tgt:
- found = True
- break
- if not found:
- return -1
- return i
- def find(seq, target):
- for i, value in enumerate(seq):
- if value == tgt:
- break
- else:
- return -1
- return i
- '''Dictionary Skills
- * Mastering dictionaries is a fundamental Python Skill
- * They are the fundamental tool for expressing relationships,
- linking, counting, and grouping. '''
- # Looping over the dictionary keys
- d = ('matthew': 'blue', 'rachel': 'green', 'raymond': 'red')
- for k in d:
- print k
- for k in d.keys():
- if k.startswith('r'):
- del d[k]
- # Looping over a dictionary keys and values
- for k in d:
- print k, '-->', d[k]
- for k, v in d.items():
- print k, '-->', v
- #====================================
- # Construct a dictionary from pairs
- names = ['raymond', 'rachel', 'matthew']
- colors = ['red', 'green', 'blue']
- d = dict(izip(names, colors))
- ('matthew': 'blue', 'rachel': 'green', 'raymond': 'red')
- # Counting with dictionaries
- colors = ['red', 'green', 'red', 'blue', 'green', 'red']
- d = {}
- for color in colors:
- if color not in d:
- d[color] = 0
- d[color] += 1
- {'blue':1, 'green':2, 'red':3}
- # simplified
- d = {}
- for color in colors:
- d[color] = d.get(color, 0) + 1
- d = defaultdict(int)
- for color in colors:
- d[color] += 1
- ### Grouping with dictionaries -- Part I
- names = ['raymond', 'rachel', 'matthew', 'roger',
- 'betty', 'melissa', 'judith', 'charlie']
- d = {}
- for name in names:
- key = len(name)
- if key not in d:
- d[key] = []
- d[key].append(name)
- # Grouping with dictionaries == Part II
- d = {}
- for name in names:
- key = len(name)
- d.setdefault(key, []).append(name)
- d = defaultdict(list)
- for name in names:
- key = len(name)
- d[key].append(name)
- """==================
- 28:00 minutes """
- # popitem
- # linking dictionaries
- # ChainMap
- # Improving Clarity
- # positional arguments and indicies
- twitter_search('@obama', retweets=False, numtweets=20, popular=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement