473,320 Members | 2,177 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Python program that counts the occurance of values in a set of data.

Hello All. I was curious if someone is willing to show me how I could write a simple python program that counts the number of times a value appears in a data list ( data that I have already written this to another file).

Much Thanks.
Jul 28 '08 #1
2 2115
bvdet
2,851 Expert Mod 2GB
List method count() returns the number of times a value or object occurs in a list.
Expand|Select|Wrap|Line Numbers
  1. >>> alist = [1,2,3,4,4,4]
  2. >>> alist.count(4)
  3. 3
  4. >>> 
Jul 28 '08 #2
If you want to count all members of the list without specifying which one to count, you could use this reasonably standard count method (this is from the Natural Language Toolkit's online Python tutorial: http://nltk.org/doc/en/programming.html)

Expand|Select|Wrap|Line Numbers
  1. alist = [1,2,3,4,4,4]
  2.  
  3. count = {}
  4. for thing in alist:        # iterates though each member of your list
  5.     if thing not in count: # checks to see if member already exists in the count
  6.         count[thing] = 0   # if a member has not been counted yet, start an entry for it                         
  7.     count[thing] += 1      # each time you see a member, add one to its count
  8.  
  9. print count
  10.  
Output = {1: 1, 2: 1, 3: 1, 4: 3}

This says "the number "1" was found once; the number "2" was found once; the number 3 was found once; the number "4" was found three times.
Jul 31 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

26
by: Christoph Zwerschke | last post by:
You will often hear that for reasons of fault minimization, you should use a programming language with strict typing: http://turing.une.edu.au/~comp284/Lectures/Lecture_18/lecture/node1.html I...
5
by: zefciu | last post by:
Hi! I want to embed a function in my python application, that creates a two-dimensional array of integers and passes it as a list (preferably a list of lists, but that is not necessary, as the...
25
by: Eric | last post by:
Hello, after reading some of the book Programming Python it seems that python is something I would like to delve deeper into. The only thing is, I have no idea what I should try and write. So I was...
4
by: sahil | last post by:
Hello frends i am learning c language, I want to make a program which count occurence of each element in an array .I write following code for it but ity is not giving me desired result.pls help me....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.