Created
April 10, 2015 14:13
-
-
Save Averroes/cb886ac987a7368ad797 to your computer and use it in GitHub Desktop.
transforming and reducing data at the same time
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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