Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 29th, 2012  |  syntax: None  |  size: 2.73 KB  |  hits: 18  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Retrieving the top value in a dictionary that has multiple values under a single key
  2. Name    Organism    Percent Match     Misc info
  3. 1        Human        100              xxx    
  4. 1        Goat          95              yyy
  5. 1        Pig           90              zzz
  6.        
  7. list = []  
  8. if "1" in line:
  9.     id = line
  10.     bsp = id.split("t")
  11.     uid = bsp[0]
  12.     per = bsp[2]
  13.  
  14.     if not dict.has_key(uid):
  15.         dict[uid] = []
  16.     dict[uid].append(per)
  17.     list = dict[uid]
  18.     list.sort()
  19. if list[0] in dict:
  20.     print key
  21.        
  22. Name    Organism    Percent Match   Misc    info
  23. 1   Human   100 xxx
  24. 1   Goat    95  yyy
  25. 1   Pig 90  zzz
  26. 2   Mouse   95  yyy
  27. 2   Moose   90  zzz
  28. 2   Manatee 100 xxx
  29.        
  30. import csv
  31.  
  32. maxrows = {}
  33. with open('test.dat', 'rb') as f:
  34.     for row in csv.DictReader(f, delimiter = 't'):
  35.         name = row['Name']
  36.         percent = int(row['Percent Match'])
  37.         if int(maxrows.get(name,row)['Percent Match']) <= percent:
  38.             maxrows[name] = row
  39.  
  40. print(maxrows)
  41.        
  42. {'1': {'info': None, 'Percent Match': '100', 'Misc': 'xxx', 'Organism': 'Human', 'Name': '1'}, '2': {'info': None, 'Percent Match': '100', 'Misc': 'xxx', 'Organism': 'Manatee', 'Name': '2'}}
  43.        
  44. lines = []
  45. with open('data.txt') as file:
  46.     for line in file:
  47.         if line.startswith('1'):
  48.             lines.append(line.split())
  49.  
  50. best_match = max(lines, key=lambda k: int(k[2]))
  51.        
  52. >>> pprint.pprint(lines)
  53. [['1', 'Human', '100', 'xxx'],
  54.  ['1', 'Goat', '95', 'yyy'],
  55.  ['1', 'Pig', '90', 'zzz']]
  56.        
  57. >>> max(lines, key=lambda k: int(k[2]))
  58. ['1', 'Human', '100', 'xxx']
  59.        
  60. with open('data.txt') as file:
  61.     best_match = max((s.split() for s in file if s.startswith('1')),
  62.                      key=lambda k: int(k[2]))
  63.        
  64. from collections import defaultdict
  65.  
  66. results = defaultdict(list)
  67. with open('data.txt') as f:
  68.     #next(f)      # you may need this so skip the header
  69.     for line in f:
  70.         splitted = line.split()
  71.         results[splitted[0]].append(splitted[1:])
  72.  
  73. maxs = {}
  74. for uid,data in results.items():
  75.     maxs[uid] =  max(data, key=lambda k: int(k[1]))
  76.        
  77. Name    Organism    Percent Match     Misc info
  78. 1        Human        100              xxx    
  79. 1        Goat          95              yyy
  80. 1        Pig           90              zzz  
  81. 2        Pig           85              zzz  
  82. 2        Goat          70              yyy
  83.        
  84. {'1': ['Human', '100', 'xxx'], '2': ['Pig', '85', 'zzz']}
  85.        
  86. with open('datafile.txt', 'r') as f:
  87.     lines = file.read().split('n')
  88.  
  89. matchDict = {}
  90.  
  91. for line in lines:
  92.     if line[0] == '1':
  93.         uid, organism, percent, misc = line.split('t')
  94.         matchDict[int(percent)] = (organism, uid, misc)
  95.  
  96. highestMatch = max(matchDict.keys())
  97.  
  98. print('{0} is the highest match at {1} percent'.format(matchDict[highestMatch][0], highestMatch))