DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Python (http://www.daniweb.com/forums/forum114.html)
-   -   List operation problem (http://www.daniweb.com/forums/thread159998.html)

hydrobat Nov 29th, 2008 9:57 am
List operation problem
 
Greetings. I'm a python noob and I'm stuck with some coding.

Say you have a list with a number of elements, some of which are the same. For instance:
mylist = ['blue', 'red', 'green', 'green', 'red', 'red']

Is there a way to return how many times each string occurs in the list?

Freaky_Chris Nov 29th, 2008 10:48 am
Re: List operation problem
 
Yes there is it's pretty simple too

mylist = ['blue', 'red', 'green', 'green', 'red', 'red']

for word in set(mylist):
    print word, "appears", mylist.count(word), "times."

Hope that helps.

Chris

hydrobat Nov 29th, 2008 11:11 am
Re: List operation problem
 
Awesome, thank you Chris!


All times are GMT -4. The time now is 1:01 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC