0
# to input the names of animals(max is 3 names)    
i=1
animals=[]
while i<=5:
    y=input('animal{}name'.format(i))
    animals.append(y)
    i+=1
    if y=='only':
        break
print('your animals are ',animals)
i=1
while i<=len(animals):
    weight1=int(input('weight for {}'.format(animals[0])))
    speed1=int(input('speed for {}'.format(animals[0])))
    food_consumption1=int(input('food consumption for{}'.format(animals[0])))
    i+=1
    if i>len(animals):
        break 
    weight2=int(input('weight for {}'.format(animals[1])))
    speed2=int(input('speed for {}'.format(animals[1])))
    food_consumption2=int(input('food consumption for{}'.format(animals[1])))
    i+=1
    if i>len(animals):
        break 
    weight3=int(input('weight for {}'.format(animals[2])))
    speed3=int(input('speed for {}'.format(animals[2])))
    food_consumption3=int(input('food consumption for{}'.format(animals[2])))
speed_list=[speed1,speed2,speed3] 
speed_list.sort(inverse=true)

now after arranging the speed of animals in a list from highspeed to low speed i need a code to allow me to arrange the animals names from fastest animal to slowest animal

  • Have a look to `pandas` ? – pyOliv May 20 '20 at 11:06
  • use sorted() list method with cmp which specifies a custom comparison function. you can arrange what ever you like acording to this predicate/or comperator function or lambda. good luck. read here [sorted](https://docs.python.org/2/library/functions.html?highlight=sorted#sorted). This is not full answer but good hint to start with. – Adam May 20 '20 at 11:13
  • i will post an answer in a minute – Adam May 20 '20 at 11:19
  • How are you storing the different animals? classes? dicts? Just different variables like in the question? – Tomerikoo May 20 '20 at 11:52
  • Does this answer your question? [Sorting list based on values from another list?](https://stackoverflow.com/questions/6618515/sorting-list-based-on-values-from-another-list) – Tomerikoo May 20 '20 at 11:55
  • @Adam ill edit the question to show u my code – Alawneh Sayf May 20 '20 at 17:39
  • @Tomerikoo i edited the question – Alawneh Sayf May 20 '20 at 18:18
  • Well the link still answers your question. Did you have a look? It's basically `[animal for speed, animal in sorted(zip(speed_list, animals))]` – Tomerikoo May 21 '20 at 07:23

1 Answers1

0
class Animal(object):
  def __init__(self, spec, name,weight, power, speed):
    self._spec=spec
    self._name=name
    self._weight=weight
    self._power=power
    self._speed = speed
  def get_speed(self):return self._speed
  def __str__(self):
    return ','.join([self._spec, self._name, str(self._weight),str(self._power),str(self._speed)])


animals_list = [
                Animal('mammal','bear',150,2.5,55), 
                Animal('mammal','dog',70,1.5,40),
                Animal('mammal','lion',260,3.5,65),
                Animal('mammal','horse',350,1,70),
                Animal('mammal','tiger',260,12,77)
              ]


new_animals_list = sorted(animals_list, key=lambda animal:animal.get_speed())
for each in new_animals_list:
  print str(each)
Adam
  • 2,820
  • 1
  • 13
  • 33