简体   繁体   中英

Python Dictionary List Issue

I'm trying to configure code to isolate these two categories:

Desired Output:

Tastiest: Limpets and Mussels
Most Involved Organism: Mussels

Tastiest (member of the food web that is eaten by the most different members of the food chain.)

Most Involved (organism that is directly linked to the largest number of organisms in the food web, either because they eat that organism or because they are eaten by that organism.)

I have written code for the Apex, Producers, and flexible eaters which is:

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)))


print (" ")

#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)


print (" ")       


#Producers
producers = []
allpreys = [item.strip() for sub in foodweb.values() for item in sub]
for p in allpreys:
    if p.strip() not in foodweb.keys() and p not in producers:
        producers.append(p)

print("The Producers Are:")
print(formatList(producers))


#Flexible Eaters

print("Most Flexible Eaters: {}".format(sorted(foodweb.items(), key=lambda x: -len(x[1]))[0][0]))

So my question is, in order to get those to configurations, would I be writing similar to the code I already have written?

For reference this is the list:

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 seems to work:

involvement = {}
taste = {}

for animal, prey in foodweb.items():
    involvement[animal] = involvement.get(animal, 0) + len(prey)
    for meal in prey:
        taste[meal] = taste.get(meal, 0) + 1
        involvement[meal] = involvement.get(meal, 0) + 1

by_taste = {}
by_inv = {}

for meal, rank in taste.items():
    by_taste.setdefault(rank, []).append(meal)

for critter, rank in involvement.items():
    by_inv.setdefault(rank, []).append(critter)

tastiest = by_taste[max(by_taste.keys())]
most_involved = by_inv[max(by_inv.keys())]


print 'Tastiest: ', tastiest
print 'Most Involved Organism: ', most_involved

(I leave the formatting to you.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM