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.  
  3. # Create a dictionary where each key is a category and each value is
  4. # a list of absence codes belonging to that category.
  5. categories = {}
  6. tapctgr.posf()
  7. while not tapctgr.readn():
  8.     category = tapctgr['GRCTG']
  9.     absence_code = tabctgr['GRATCD']
  10.     if category in categories:
  11.         categories[category].append(absence_code)
  12.     else:
  13.         categories[category] = [absence_code]
  14. tapctgr.close()
  15.  
  16. # Create a dictionary where each key is a unique combination of absence
  17. # codes and each value is a list of categories that consist of that
  18. # combination.
  19. content_table = {}
  20. for category in categories:
  21.     categories[category].sort()
  22.     contents = categories[category]
  23.     if contents in content_table:
  24.         content_table[contents].append(category)
  25.     else:
  26.         content_table[contents] = [category]
  27.  
  28. # Print the categories which are exact duplicates.
  29. for contents in content_table:
  30.     if len(content_table[contents]) > 1:
  31.         print contents, 'are in', content_table[contents]
  32.  
© 2004-2019 by midrange.com generated in 0.007s valid xhtml & css