Distinct Vlaues

Thread Solved

Join Date: Oct 2009
Posts: 5
Reputation: kenmeck03 is an unknown quantity at this point 
Solved Threads: 0
kenmeck03 kenmeck03 is offline Offline
Newbie Poster

Distinct Vlaues

 
0
  #1
29 Days Ago
Write a function called NumDistinctDict that takes a single parameter alist that determines the number of distinct values in alist. For example, the list [1,2,4,2,7] contains 3 unique values (namely 1,2,4 and 7).
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 46
Reputation: Mathhax0r is an unknown quantity at this point 
Solved Threads: 11
Mathhax0r Mathhax0r is offline Offline
Light Poster
 
0
  #2
29 Days Ago
This sounds suspiciously like homework, so I'll just give you a hint.

someList.index(item) returns the index of the item if it's in some list. Otherwise, it'll throw ValueError.

Example:
  1. listA = [1,2,3,5]
  2. try:
  3. listA.index(4)
  4. print "Four is in the list."
  5. except ValueError:
  6. print "Four isn't in the list."
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,008
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 285
woooee woooee is offline Offline
Veteran Poster
 
0
  #3
29 Days Ago
This is very simple to do.
  1. print len(set([1,2,4,2,7]))
Linux counter #99383
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 200
Reputation: masterofpuppets is an unknown quantity at this point 
Solved Threads: 47
masterofpuppets's Avatar
masterofpuppets masterofpuppets is offline Offline
Posting Whiz in Training
 
0
  #4
28 Days Ago
Originally Posted by woooee View Post
This is very simple to do.
  1. print len(set([1,2,4,2,7]))
that's really useful...

you could also do it in the old fashion way

  1. def NumDistinctDict( l ):
  2. distinct = []
  3. for num in l:
  4. if num not in distinct:
  5. distinct += [ num ]
  6. return len( distinct )
My site ->> http://8masterofpuppets8.webs.com/
"My belief is stronger than your doubt."
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,004
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 928
Moderator
vegaseat's Avatar
vegaseat vegaseat is online now Online
DaniWeb's Hypocrite
 
0
  #5
28 Days Ago
Let's hope it wasn't homework!
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

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



Other Threads in the Python Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC