Feb-20-2021, 05:48 AM
Using a list you would need two lists, animals and animal counts.
to_app ='C:/02/All_an.txt' animals = ['dog','cat','fish'] # Never use list as a variable name counts = [0, 0, 0] with open (to_app, 'r+') as app: for line in app : line = line.strip() for index, animal in enumerate(animals): if animal in line: parts = line.split(",") count[index] += int(parts[2]) break for animal, count in zip(animals, counts): print(animal, count)This code is hideous because there is no association between the count and the animal other than the position in the list. Since Python provides a datatype who's entire reason for existing is to provide a tight coupling, may as well use that.