I have a working code, but I was wondering if there was a better way to do it.
The function gets the sum of appearances for each specified item and returns them as a list.
def question10(ipaddresses: list[str]):
none_count = sum(1 if x is None else 0 for x in ipaddresses)
host_count = sum(1 if x == '129.128.1.1' else 0 for x in ipaddresses)
target_count = sum(1 if x == '192.128.1.4' else 0 for x in ipaddresses)
return [none_count, host_count, target_count]
You could use Counter from the collections library.
It would look something like this:
Note that the
list[str]syntax is only available in newer python versions like Python 3.9 and later.For older versions you'd use something like:
The usecase for these, however, will remain the same. For example the list:
should output
[0, 2, 1]in both cases