🎉 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?
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]
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 -- ¦þ
MySite
______________________________________________________________
Game of the day: eliminate the lambda
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]
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]
>>> 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]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement