Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 8th, 2012, 08:42 PM
Nick Wyden Nick Wyden is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 44 Nick Wyden Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 4 h 10 m 54 sec
Reputation Power: 0
Question about lists

Hi, what could I do if I wanted to find out how many times a certain value appeared in a list. For example how could I find out how many times "Z" appeared in list 1

thanks

Reply With Quote
  #2  
Old November 8th, 2012, 10:16 PM
Lux Perpetua Lux Perpetua is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2004
Location: San Francisco Bay
Posts: 1,938 Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 2 h 43 m 16 sec
Reputation Power: 1312
Code:
>>> from collections import defaultdict
>>> d = defaultdict(int)
>>> a = ['a', 'b', 'c', 'a', 'b', 'd']
>>> for x in a:
...     d[x] += 1
...     
>>> d['a']
2
>>> d['b']
2
>>> d['c']
1
>>> d['d']
1
This counts the occurrences of every value in the list, assuming each value is hashable. If you only care about one value, I think you're better off manually comparing each list item to the value and incrementing a counter when equality occurs.

Reply With Quote
  #3  
Old November 8th, 2012, 10:31 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,458 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 4 Days 6 h 26 m 43 sec
Reputation Power: 403
I'd use the interpreter. Browsing through the output of

>>> help(list())

I'd find a count method. And then I'd learn more detail:

>>> help(list().count)

And maybe I'd look in the iterables or list section of the module library reference at docs.python.org .

And I'd practice with a few cases back in the interpreter to make sure the funny variations I can think of work in reasonable ways.

And then I'd be ready to use the list.count method.
Comments on this post
Lux Perpetua agrees: This is certainly simpler than my methods.
__________________
[code]Code tags[/code] are essential for python code!

Reply With Quote
  #4  
Old November 9th, 2012, 06:55 PM
Nick Wyden Nick Wyden is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 44 Nick Wyden Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 4 h 10 m 54 sec
Reputation Power: 0
alright thanks

Reply With Quote
  #5  
Old November 9th, 2012, 08:36 PM
KING-OLE KING-OLE is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Location: Texas
Posts: 24 KING-OLE User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 54 m 45 sec
Reputation Power: 0
Here's an example of what b49P23TIvg was referring to:

Code:
>>> a=['A','B','A','C']
>>> a.count('A')
2
>>> a.count('B')
1
>>> a.count('E')
0
>>> for i in range (0,a.count('A')):
...   e = a.index('A',i)
...   print ("element ",e," is the letter ",a[e])
...
element  0  is the letter  A
element  2  is the letter  A
>>>

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Question about lists

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap