🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

how to search a list for specific characters?

Started by
7 comments, last by xegoth 20 years, 4 months ago
I want to use count(x) to see how many times a letter occurs in a list I have. For example the list might be stuff = ['orange','apple','pear'] And I want to know how many A's are in the list. any idea how I might go about doing that? If I use stuff.count('a') it doesn't work (I guess it has to match the entire string). Anyone know how to count how many times a single letter occurs in a list of strings? I'm using Python Thanks in advance, [edited by - xegoth on February 15, 2004 11:13:51 PM]
Advertisement
What language is this?
You might want to specify what language you''re using.

______________________________________________________________
The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ
MySite
______________________________________________________________
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Formats______________________________________________________________________________________
lol, oh yeah. Python.
def countCharacter( ch, L = [] ):   return sum( map(lambda x: x.count(ch), L) ) 
Game of the day: eliminate the lambda
def countCharacter( ch, L = [] ):      return sum( map( string.count, L, ch*len(L) ) )  


edit - fixed

“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” — Brian W. Kernighan (C programming language co-inventor)

[edited by - Fruny on February 16, 2004 3:50:38 AM]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
That would be string.count, not list.count.
>>> stuff = [''orange'',''apple'',''pear'']>>> ''''.join(stuff).count(''a'')3>>> ''''.join(stuff).count(''n'')1>>> ''''.join(stuff).count(''z'')0


[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]
Kylotan, you''re evil! I need to work more closely with you...

This topic is closed to new replies.

Advertisement