Skip to content

Instantly share code, notes, and snippets.

@Averroes
Created April 10, 2015 14:13
Show Gist options
  • Select an option

  • Save Averroes/cb886ac987a7368ad797 to your computer and use it in GitHub Desktop.

Select an option

Save Averroes/cb886ac987a7368ad797 to your computer and use it in GitHub Desktop.
transforming and reducing data at the same time
# example.py
#
# Some examples of using generators in arguments
import os
files = os.listdir(os.path.expanduser('~'))
if any(name.endswith('.py') for name in files):
print('There be python!')
else:
print('Sorry, no python.')
# Output a tuple as CSV
s = ('ACME', 50, 123.45)
print(','.join(str(x) for x in s))
# Data reduction across fields of a data structure
portfolio = [
{'name':'GOOG', 'shares': 50},
{'name':'YHOO', 'shares': 75},
{'name':'AOL', 'shares': 20},
{'name':'SCOX', 'shares': 65}
]
min_shares = min(s['shares'] for s in portfolio)
print(min_shares)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment