I store data in a dictionary where the key is an integer and the value is a tuple of integers. I need to get the length of the longest element and its key.
I found this to get the maximum value over the dict:
def GetMaxFlow(flows):
maks=max(flows, key=flows.get)
return flows[maks],maks
I tried to change and use the function as the key len, but this did not work, so I tried something sensible and direct, but ineffective:
def GetMaxFlow(flows):
Lens={}
for a in flows.iteritems():
Lens[a[0]]=len(a[1])
maks=max(Lens, key=Lens.get)
return Lens[maks],maks
Is there a more elegant and pythonic way to do this?
source
share