Can someone please show me the best way to achieve this with the least amount of lines? Im a recovering PHP coder, I have one solution. I was wondering if there was a quicker more pythony way to do this:
I have in "results" [('Basp1', 'Aen2'), ('Basp1', 'Ahy18'), ('Basp1', 'Ahy26'), ('Yeps1', 'Eco1'), ('Yeps1', 'Ahy7'), ('Yeps1', 'Asa1'), ('Cagi1', 'Ahy15'), ('Cagi1', 'Ahy24'), ('Cagi1', 'Ahy31'), ('Bado1', 'Aen2'), ('Bado1', 'Eco1'), ('Bado1', 'Ahy38'), ('Rhle1', 'Ahy2'), ('Rhle1', 'Aen2'), ('Rhle1', 'Aen1'), ('Prma1', 'Aen2'), ('Prma1', 'Aen1'), ('Prma1', 'Ahy26'), ('Cysp1', 'Aen2'), ('Cysp1', 'Aen1'), ('Cysp1', 'Ahy26'), ('Basp2', 'Aen2'), ('Basp2', 'Eco1'), ('Basp2', 'Ahy38'), ('Naph1', 'Eco1'), ('Naph1', 'Ahy40'), ('Naph1', 'Ahy30'), ('Phte1', 'Eco1'), ('Phte1', 'Ahy2'), ('Phte1', 'Aen2'), ('Phte1', 'Aen2'), ('Sepr1', 'Ahy2'), ('Sepr1', 'Eco1'), ('Sepr1', 'Ahy23'), ('Mesp1', 'Asa1'), ('Mesp1', 'Ahy33'), ('Mesp1', 'Ahy34'), ('Prsp1', 'Aen2'), ('Prsp1', 'Aen1'), ('Prsp1', 'Ahy26'), ('Brsp2', 'Aen2'), ('Brsp2', 'Aen1'), ('Brsp2', 'Ahy2'), ('Lapl1', 'Aca1'), ('Lapl1', 'Eco1'), ('Lapl1', 'Ave1')]
The first values of each 3 consecutive tuples are the same.
I want to build a dictionary that looks like this
keys = {Basp1: [Aen2,Ahy18,Ahy26]} etc...
Right now I have two solutions:
for pairs in results:
if keys.has_key(pairs[0]):
keys[pairs[0]].append(pairs[1])
else:
keys[pairs[0]]=[pairs[1]]
for pairs in results:
try:
keys[pairs[0]].append(pairs[1])
except:
keys[pairs[0]]=[pairs[1]]
Thanks!