Skip to content

Instantly share code, notes, and snippets.

@attheodo
Created February 11, 2024 18:08
Show Gist options
  • Save attheodo/4af0af103bf5943eb1e9d69789fb8a0d to your computer and use it in GitHub Desktop.
Save attheodo/4af0af103bf5943eb1e9d69789fb8a0d to your computer and use it in GitHub Desktop.
def count_genus_species(file_path):
genus_species = {}
total_species = 0
total_genuses = 0
with open(file_path, 'r') as file:
for line in file:
genus = line.split()[0]
species = line.split()[-1]
if genus in genus_species:
genus_species[genus].append(species)
else:
genus_species[genus] = [species]
total_genuses += 1
total_species += 1
average_species_per_genus = total_species / total_genuses
return total_genuses, average_species_per_genus
file_path = 'organisms.txt'
unique_genuses, average_species = count_genus_species(file_path)
print("Number of unique genuses:", unique_genuses)
print("Average number of species per genus:", average_species)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment