Apologies for the somewhat vague title, I’ll try explain more here.
Currently, I have the following code, which counts the number of times the values “y” and “n” show up in the list called “results”.
NumberOfA = results.count("y")
NumberOfB = results.count("n")
Is there a way of making it so that, for example, values such as “yes” are also counted towards NumberOfA? I was thinking something along the lines of the following:
NumberOfA = results.count("y" and "yes" and "Yes")
NumberOfB = results.count("n" and "no" and "No")
But that doesn’t work. This is probably a pretty easy one to solve, but hey. Thanks in advance!
As for why your answer above does not work, it is because Python will just take the final value of the expression you pass in:
Therefore your
countwill be off because it is just looking for that final value:Would something like this work? Note that this depends on their being only yes/no-esque answers in the list (will be wrong if things like ‘Yeah….um no’ are in there):
The general idea is to take your results list and then iterate through each item, lowercasing it and then taking the first letter (
startswith) – if the letter is ay, we know it is ayes; otherwise, it will beno.You can also combine the steps above if you want by doing something like this (note this requires Python 2.7):
Counterobjects can be treated just like dictionaries, so you would now essentially have a dictionary that contained the counts ofyes‘s andno‘s.