I'm trying to take a text file with a list in it take the input values and order them in an appropriate output format however I'm having difficulty trying to create a loop and create this dictionary.
I have created a blank dictionary however I'm not sure I'm supposed to create a dictionary like in order to get the output:
foodweb =
{
“Bird”: [
Prawn,
Mussels,
Crab,
Limpets,
Whelk
],
“Crab”: [
Mussels,
Limpets
],
“Fish”: [
Prawn
],
“Limpets”: [
Seaweed
],
“Lobster”: [
Crab,
Mussels,
Limpets,
Whelk
],
“Mussels”: [
Phytoplankton,
Zooplankton
],
“Prawn”: [
Zooplankton
],
“Whelk”: [
Limpets,
Mussels
],
“Zooplankton”: [
Phytoplankton
]
}
How can I write a loop to make this more simple?!?
file_content = open('AquaticFoodWeb.txt' , 'r')
aquaticfoodweb ={}
for line in file_content:
myList=line.rstrip('\n').split(" eats ")
This is the list from the txt file
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
This is the desired output format I'm striving for:
Predators and Prey:
Bird eats Prawn, Mussels, Crab, Limpets and Whelk
Crab eats Mussels and Limpets
Fish eats Prawn
Limpets eats Seaweed
Lobster eats Crab, Mussels, Limpets and Whelk
Mussels eats Phytoplankton and Zooplankton
Prawn eats Zooplankton
Whelk eats Limpets and Mussels
Zooplankton eats Phytoplankton