-1
def consensus(seqs):    
>>> profiel(seqs)
    {'A': [0, 3, 1, 2, 3, 4, 0, 3], 'C': [0, 4, 3, 1, 0, 0, 5, 1], 'T': [3, 0, 1, 2, 4, 3, 1, 1], 'G': [4, 0, 2, 2, 0, 0, 1, 2]}
>>> consensus(profiel(seqs))
    'GCCNTACA'

How can I return the key of the dictionary when it have the highest number in the list? So the first one is a G because

A = 0 , C = 0, T = 3, G = 4

And so on.

Victor
  • 65
  • 7

5 Answers5

1

First, you can use the key keyword of the max function (python max function using 'key' and lambda expression). Then, you can use a generator expression to iterate through the lists and join it into a string.

dic = {'A': [0, 3, 1, 2, 3, 4, 0, 3], 'C': [0, 4, 3, 1, 0, 0, 5, 1], 'T': [3, 0, 1, 2, 4, 3, 1, 1], 'G': [4, 0, 2, 2, 0, 0, 1, 2]}
''.join(max(dic, key=lambda v: dic[v][i]) for i in xrange(len(dic['A'])))
### 'GCCATACA'

Note that this only works if all lists have the same number of elements.

Community
  • 1
  • 1
Julien Spronck
  • 15,069
  • 4
  • 47
  • 55
  • same comment as other answer, there is a 5 in C how can the biggest be G? – Netwave Nov 18 '15 at 11:08
  • @DanielSanchez I think you might have misunderstood the question ... as far as I can tell, the op is not looking for the key with the highest number but a list of keys. At each index i of that list, the key k is the one for which dic[k][i] is maximal. ... I think – Julien Spronck Nov 18 '15 at 11:11
  • Yep, was talking with Borja about what he actually wants, i downvoted the question for that. – Netwave Nov 18 '15 at 11:13
  • @Victor does my code give you what you need or did I misunderstand the question? – Julien Spronck Nov 18 '15 at 11:17
  • I think so but is xrange() the same as range()? Because my python doesn't know what xrange() is. – Victor Nov 18 '15 at 11:19
  • @Victor that depends what version of python you are using ... in python 2.7, xrange is the generator version of range ... in any case, you can replace it by range. however, in python 2.7, xrange is usually preferable since you do not put the entire list in memory but just one item at a time. – Julien Spronck Nov 18 '15 at 11:20
0

To select key with max value, use this

max(dict, key=dict.get)  

Example:

item = d[d.keys()[0]]

''.join(
    max(d, key=lambda key: d.get(key)[i]) for i in range(len(item))
)
Kenly
  • 24,317
  • 7
  • 44
  • 60
0

This will do the job, key of the list with the highest value on it:

mydict = {'A': [0, 3, 1, 2, 3, 4, 0, 3], 'C': [0, 4, 3, 1, 0, 0, 5, 1], 'T': [3, 0, 1, 2, 4, 3, 1, 1], 'G': [4, 0, 2, 2, 0, 0, 1, 2]}

def getKeyBigger(d);
    key,_ = reduce(lambda x, y: x if max(x[1]) > max(y[1]) else y, d.items())
    return key

>>>print getKeyBigger(mydict)
>>>C
Netwave
  • 40,134
  • 6
  • 50
  • 93
0
def return_max(dic): 
    maxnum = max(i[0] for i in dic.values())        
    for i in dic.iteritems():
        if maxnum == i[1][0]:
            return i[0]

dic = {'A': [0, 3, 1, 2, 3, 4, 0, 3], 'C': [0, 4, 3, 1, 0, 0, 5, 1], 'T': [3, 0, 1, 2, 4, 3, 1, 1], 'G': [4, 0, 2, 2, 0, 0, 1, 2]}
print return_max(dic)

Output:

G
Avión
  • 7,963
  • 11
  • 64
  • 105
0

Based on zetsys solution, here's a (ugly) one liner:

"".join([max(d, key={k: v[x] for k, v in d.items()}.get) for x in xrange(len(d.values()[0]))]

It makes the assumption that every list has the same length. (and d is your input dictionary)

ohe
  • 3,461
  • 3
  • 26
  • 50