Map transformation example from p 18 in Oreilly's Programming Collective Intelligence
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
{'Lisa Rose': {'Lady in the Water': 2.5, 'Snakes on a Plane': 3.5}, | |
'Gene Seymour': {'Lady in the Water': 3.0, 'Snakes on a Plane': 3.5}} | |
to: | |
{'Lady in the Water':{'Lisa Rose':2.5,'Gene Seymour':3.0}, | |
'Snakes on a Plane':{'Lisa Rose':3.5,'Gene Seymour':3.5}} | |
def transformPrefs(prefs): | |
result={} | |
for person in prefs: | |
for item in prefs[person]: | |
result.setdefault(item,{}) | |
# Flip item and person | |
result[item][person]=prefs[person][item] | |
return result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment