Trying to determine the "height" of organisms using Python code

I'm trying to take a text file with a list in it, taking the input values ​​and evaluating them to "Height", which means that any animal that eats only the producer has a height of 1.

I think I have a little understanding of how to solve this problem:

  • height adjustment of all organisms, including producers, to 0
  • note that something has changed.
  • so far something has changed ...
    • note that nothing has changed
    • for each animal a, in the food web
      • for every animal pthat apreys on
        • If height a is less than or equal to height p, set height a to height and note that something has changed.

Any help is greatly appreciated. I hope I was not too verbose or confused my message.

foodweb = {}

with open('AquaticFoodWeb.txt') as input:
    for line in input:
        animal, prey = line.strip().split(' eats ')
        foodweb.setdefault(animal, []).append(prey)

print ("Predators and Prey:")

for animal, prey in sorted(foodweb.items()):
    if len(prey) > 1:
        print ("{} eats {} and {}".format(animal, ", ".join(prey[:-1]),     prey[-1]))
    else:
        print ("{} eats {}".format(animal, ", ".join(prey)))

#Apex

values = [item.strip() for sub in foodweb.values() for item in sub]
for apex in foodweb.keys():
    if apex.strip() not in values:
        print("Apex Predators: ", apex)

This is text file input:

Bird eats Prawn
Bird eats Mussels
Bird eats Crab
Bird eats Limpets
Bird eats Whelk
Crab eats Mussels
Crab eats Limpets
Fish eats Prawn
Limpets eats Seaweed
Lobster eats Crab
Lobster eats Mussels
Lobster eats Limpets
Lobster eats Whelk
Mussels eats Phytoplankton
Mussels eats Zooplankton
Prawn eats Zooplankton
Whelk eats Limpets
Whelk eats Mussels
Zooplankton eats Phytoplankton

Requires conclusion:

 Heights:
  Bird: 4
  Crab: 3
  Fish: 3
  Limpets: 1
  Lobster: 4
  Mussels: 2
  Phytoplankton: 0
  Prawn: 2
  Seaweed: 0
  Whelk: 3
  Zooplankton: 1
+4
source share
2 answers

I hope this is not homework, the code below can solve your problem.

animals=[]
preys=[]

with open('AquaticFoodWeb.txt','r') as f:
    for line in f:
        animals.append(line.split()[0])
        preys.append(line.split()[-1])

height = {}

length = len(preys)
rank = 0
while preys != [None]*length:
    for index,(animal,prey) in enumerate(zip(animals,preys)):
        if prey not in animals:
            try:
                if height[prey] < rank:
                    height[prey] = rank
            except KeyError:
                height[prey] = 0

            height[animal] = height[prey] + 1

            preys[index] = None
            animals[index] = None
    rank += 1

print sorted(height.items(),key = lambda x:x[1],reverse=True)

Output:

[('Lobster', 4), ('Bird', 4), ('Fish', 3), ('Whelk', 3), ('Crab', 3),('Mussels', 2), 
('Prawn', 2), ('Zooplankton', 1), ('Limpets', 1),('Phytoplankton', 0), ('Seaweed', 0)]
+1
source

This is not a bad start. My analysis of what he needs:

For each organism, the height is more than the maximum height of the organisms that it eats. Seaweed and phytoplankton are 0 because they do not eat anything. Zooplankton and Limpet are 1 because they only eat level 0 organisms.

, , . , , , , :

with open('AquaticFoodWeb.txt') as input:
    for line in input:
        animal, prey = line.strip().split(' eats ')
        foodweb.setdefault(animal, []).append(prey)
        foodweb.setdefault(prey, [])  # new line of code

, , , .

- foodweb , . , 0 , . , , height plus one, foodweb.

-0 , . , . , pass, foodweb , .

+2

Source: https://habr.com/ru/post/1618257/


All Articles