- Retrieving the top value in a dictionary that has multiple values under a single key
- Name Organism Percent Match Misc info
- 1 Human 100 xxx
- 1 Goat 95 yyy
- 1 Pig 90 zzz
- list = []
- if "1" in line:
- id = line
- bsp = id.split("t")
- uid = bsp[0]
- per = bsp[2]
- if not dict.has_key(uid):
- dict[uid] = []
- dict[uid].append(per)
- list = dict[uid]
- list.sort()
- if list[0] in dict:
- print key
- Name Organism Percent Match Misc info
- 1 Human 100 xxx
- 1 Goat 95 yyy
- 1 Pig 90 zzz
- 2 Mouse 95 yyy
- 2 Moose 90 zzz
- 2 Manatee 100 xxx
- import csv
- maxrows = {}
- with open('test.dat', 'rb') as f:
- for row in csv.DictReader(f, delimiter = 't'):
- name = row['Name']
- percent = int(row['Percent Match'])
- if int(maxrows.get(name,row)['Percent Match']) <= percent:
- maxrows[name] = row
- print(maxrows)
- {'1': {'info': None, 'Percent Match': '100', 'Misc': 'xxx', 'Organism': 'Human', 'Name': '1'}, '2': {'info': None, 'Percent Match': '100', 'Misc': 'xxx', 'Organism': 'Manatee', 'Name': '2'}}
- lines = []
- with open('data.txt') as file:
- for line in file:
- if line.startswith('1'):
- lines.append(line.split())
- best_match = max(lines, key=lambda k: int(k[2]))
- >>> pprint.pprint(lines)
- [['1', 'Human', '100', 'xxx'],
- ['1', 'Goat', '95', 'yyy'],
- ['1', 'Pig', '90', 'zzz']]
- >>> max(lines, key=lambda k: int(k[2]))
- ['1', 'Human', '100', 'xxx']
- with open('data.txt') as file:
- best_match = max((s.split() for s in file if s.startswith('1')),
- key=lambda k: int(k[2]))
- from collections import defaultdict
- results = defaultdict(list)
- with open('data.txt') as f:
- #next(f) # you may need this so skip the header
- for line in f:
- splitted = line.split()
- results[splitted[0]].append(splitted[1:])
- maxs = {}
- for uid,data in results.items():
- maxs[uid] = max(data, key=lambda k: int(k[1]))
- Name Organism Percent Match Misc info
- 1 Human 100 xxx
- 1 Goat 95 yyy
- 1 Pig 90 zzz
- 2 Pig 85 zzz
- 2 Goat 70 yyy
- {'1': ['Human', '100', 'xxx'], '2': ['Pig', '85', 'zzz']}
- with open('datafile.txt', 'r') as f:
- lines = file.read().split('n')
- matchDict = {}
- for line in lines:
- if line[0] == '1':
- uid, organism, percent, misc = line.split('t')
- matchDict[int(percent)] = (organism, uid, misc)
- highestMatch = max(matchDict.keys())
- print('{0} is the highest match at {1} percent'.format(matchDict[highestMatch][0], highestMatch))