Reputation: 19
Im trying to make a function that has a parameter which is a list of 4-element lists, that represents which team people approve. The order of the inner( 4-element) list is ['team1', 'team2', 'team3', 'team4']
People will vote 'YES' for at least one teamand 'NO' for the other three. Each 'YES'counts for one vote.
The output is a list with the total amount of 'YES' for each team in the same order of the original order.
An example would be:
electing([['YES', 'NO', 'NO', 'NO'],['NO', 'NO', 'NO', 'YES'], ['YES', 'NO','NO','NO']])
which would return ([2,0,0,1])
Can you help me please..Im new to python and I just only made it to getting each persons vote count like [1,0,0,0] but I couldnt add each list to make one list.
Would appreciate the help.
Upvotes: 0
Views: 71
Reputation: 22007
Python doesn't have a for
loop like most languages, and that can be a bit confusing to new users of this language. In cases like this (where you want to count from zero to some arbitrary length) you can use the range
function:
for i in range(4):
...
That is (roughly) the same as:
for i in [0,1,2,3]:
...
Use this and try to formulate your code, if you have more questions show what you've tried so far and we might be able to help you more.
P.S. For a more advanced way of doing what you want, I'd suggest looking at the sum
and zip
built-ins, as well as the concept of list comprehensions. Those can reduce your task to an one-liner...
Upvotes: 0
Reputation: 304137
vote1 = ['YES', 'NO', 'NO', 'NO']
vote2 = ['NO', 'NO', 'NO', 'YES']
vote3 = ['YES', 'NO','NO','NO']
votelist = [vote1, vote2, vote3]
def electing(votelist):
return [votes.count("YES") for votes in zip(*votelist)]
Upvotes: 1