List operation problem

Thread Solved
Reply

Join Date: Nov 2008
Posts: 12
Reputation: hydrobat is an unknown quantity at this point 
Solved Threads: 0
hydrobat hydrobat is offline Offline
Newbie Poster

List operation problem

 
0
  #1
Nov 29th, 2008
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?
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: List operation problem

 
0
  #2
Nov 29th, 2008
Yes there is it's pretty simple too

  1. mylist = ['blue', 'red', 'green', 'green', 'red', 'red']
  2.  
  3. for word in set(mylist):
  4. print word, "appears", mylist.count(word), "times."

Hope that helps.

Chris
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 12
Reputation: hydrobat is an unknown quantity at this point 
Solved Threads: 0
hydrobat hydrobat is offline Offline
Newbie Poster

Re: List operation problem

 
0
  #3
Nov 29th, 2008
Awesome, thank you Chris!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC