Created
October 28, 2011 08:39
-
-
Save sgammon/1321881 to your computer and use it in GitHub Desktop.
Experiments with Python datastructures
This file contains 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
## Sample data | |
simplelist = [1, 2, 3, 4, 5] | |
listsample = ['Sam', 'Tyler', 'David', 'AJ'] | |
people = [('Sam', 'Gammon'), ('Tyler', 'Porras'), ('David', 'Rekow'), ('Jackson', 'Harris')] | |
merchandise = { | |
'tshirts': ('SurviveSac T-Shirt', {'price': 5.99, 'orders': 15, 'options': ['small', 'large', 'xlarge']}), | |
'map': ('Field of Play Map', {'price': 3.0, 'orders': 28}), | |
'tickets': ('Ticket to Play', {'price': 5.0, 'orders': 1428}) | |
} | |
## Simple loops | |
for person in listsample: | |
print person | |
## Sample map job | |
for line in map(lambda x: str(x[0])+': '+str(x[1]), enumerate(reversed(people[0:-1]))): | |
print line | |
## Sample reduce job | |
print 'reduce: '+str(reduce(lambda x, y: x+y, simplelist)) | |
## Sample tuple unpacking | |
for firstname, lastname in people: | |
print 'f: ', firstname, ' l: ', lastname | |
## Sample dict unpacking | |
for merchandise_item_code, merchandise_item in merchandise.items(): | |
print '(item %s)' % merchandise_item_code | |
merchandise_item_name, merchandise_item_details = merchandise_item | |
print '---Name: '+merchandise_item_name | |
for detail_name, detail_value in merchandise_item_details.items(): | |
print '---'+detail_name.capitalize()+': '+str(detail_value) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment