I have a solution using Counter for you:
import collections
data = """
---------------------------------
Color: red/test/base
person: latest
---------------------------------
Color: red-img-tests
person: latest
---------------------------------
Color: red-zero-tests
person: latest
---------------------------------
Color: red-replication-tests
person: latest
---------------------------------
Color: blue
person: latest
---------------------------------
Color: black/red-config-img
person: 7e778bb
person: 82307b2
person: 8731770
person: 7777aae
person: 081178e
person: c01ba8a
person: 881b1ad
person: d2fb1d7
---------------------------------
Color: black/pasta
person: latest
---------------------------------
Color: black/base-img
person: 0271332
person: 70da077
person: 3700c07
person: c2f70ff
person: 0210138
person: 083af8d
"""
print (data)
colors = ["black", "red", "blue"]
final_count = []
for line in data.split("\n"):
for color in colors:
if color in line:
final_count.append(color)
#break # Uncomment this break if you don't want to count
# two colors in the same line
final_count = collections.Counter(final_count)
print(final_count)
Output
Counter({'blue': 1, 'black': 3, 'red': 5})
Here‘s the link to Python official documentation and a quick reference:
This module implements specialized container datatypes providing
alternatives to Python’s general purpose built-in containers, dict,
list, set, and tuple.
9
solved in python how to count how many times certain words appear without specifying the word [duplicate]