Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 21 Online
OpenStudy (anonymous):

If I want to count how many times a word appears in a nested list, what function do I use? My list looks like: Top = [[ 'White', 'White', 'White'] , [ 'White' , 'White', 'White' ] , [ 'White' , 'White', 'White' ]] But Top.count('White') -> 0 I want to get it to say 9

OpenStudy (anonymous):

well, you would have to write your own function and you could use the count() method there will not be a single built-in function or list method that will do what you want your function will require iteration

OpenStudy (anonymous):

code below prints 9 Top = [[ 'White', 'White', 'White'] , [ 'White' , 'White', 'White' ] , [ 'White' , 'White', 'White' ]] count=0 for i in range(0,len(Top)): count += Top[i].count('White') print count

OpenStudy (anonymous):

well .... more spoilers: http://dpaste.com/810932/

OpenStudy (anonymous):

@bwCA Interesting variations.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!