Need to sort HOST string array by its associated float speed
Is there nice way to get array from sorted tuples? Can I eliminate ordered array?
#!/usr/bin/env python
def getSpeed(url):
if url == 'h1':
return 0.522611856461
elif url == 'h2':
return 0.438368797302
elif url == 'h3':
return 0.443703174591
def orderHosts():
hostInfo = []
ordered = []
hosts = ['h1', 'h2', 'h3']
for host in hosts:
hostInfo.append( (host, getSpeed(host)) )
print hostInfo
return ordered
if __name__ == "__main__":
print("orderedHosts {}".format(orderHosts()))
output:
[('h1', 0.522611856461), ('h2', 0.438368797302), ('h3', 0.443703174591)]
orderedHosts []
dictionary? What exacly are you trying to do?hostInfocould be a dictionary instead of array in which I am appending tuples, so append would become likehostInfo[speed] = host. I thought it would easier to sort tuples.