midrange.com code scratchpad
Name:
categories.py
Scriptlanguage:
Python
Tabwidth:
4
Date:
06/16/2011 10:05:39 pm
IP:
Logged
Description:
Python program for Dennis's task (Subject: Identifying unique sets of combinations [posted Fri, 03 Jun 2011 12:40:12 -0400])
Code:
  1. tapctgr = File400('TAPCTGR', 'r')
  2. # Create a dictionary where each key is a category and each value is
  3. # a list of absence codes belonging to that category.
  4. categories = {}
  5. tapctgr.posf()
  6. while not tapctgr.readn():
  7.     category = tapctgr['GRCTG']
  8.     absence_code = tabctgr['GRATCD']
  9.     if category in categories:
  10.         categories[category].append(absence_code)
  11.     else:
  12.         categories[category] = [absence_code]
  13. tapctgr.close()
  14.  
  15. # Create a dictionary where each key is a unique combination of absence
  16. # codes and each value is a list of categories that consist of that
  17. # combination.
  18. content_table = {}
  19. for category in categories:
  20.     categories[category].sort()
  21.     contents = categories[category]
  22.     if contents in content_table:
  23.         content_table[contents].append(category)
  24.     else:
  25.         content_table[contents] = [category]
  26.  
  27. # Print the categories which are exact duplicates.
  28. for contents in content_table:
  29.     if len(content_table[contents]) > 1:
  30.         print contents, 'are in', content_table[contents]
© 2004-2019 by midrange.com generated in 0.008s valid xhtml & css