Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8324971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:16:15+00:00 2026-06-09T00:16:15+00:00

I am a Python beginner trying to count the number of certain sizes in

  • 0

I am a Python beginner trying to count the number of certain sizes in a big data set. The original data is in a text file separated by tabs. I have “Names” (string, but each row seems like a list) of different animals and “Sizes” (integer number) of them in a different row. I would like to count the number of all the animals that fall in certain size ranges, between 10-30.

So far, I have successfully counted how many of each “Name” I have but failing in specifying the “Size”. The code I have is below, and I dot get any error but it just gets ignored. Could somebody please help me why the codes are being ignored? Thank you for your help in advance!

import csv, collections

reader=csv.reader(open('C:\Users\Owl\Desktop\Data.txt','rb'), delimiter='\t')
counts=collections.Counter()

for line in reader:
   Name=line[1]
   Size=line[10]
   counts[Name]+=1

for (Name, count) in counts.iteritems():
   if 10<=Size<=30:
      print '%s: %s' % (Name, count)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T00:16:17+00:00Added an answer on June 9, 2026 at 12:16 am

    As written, Size will be permanently set to the last size value in the file, it’s not stored along with Name.

    Each round through the for loop, Size is set to line[10], but it’s not stored in anything outside of the scope of the loop. Name is indirectly stored in the counter. So the next time the loop runs, the value of Size changes to the next animal’s size.

    Does each animal appear more than once in the data?

    You will either need a slightly more complex data structure or to look at the size while looping through the file.

    If you don’t mind ignoring the animals outside of the size range:

    for line in reader:
        size = float(line[10])
        if 10 <= size <= 30:
            name = line[1]
            counts[name] += 1
    
    for name, count in counts.iteritems():
        print '%s: %s' % name, count
    

    (Note: I’ve changed the case and whitespace of your original code to match Python’s recommended style guide, pep8.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Python beginner here. I have a text file that is sorted into columns: fields
I'm a beginner in python and I'm trying to use a octal number in
I am quite a beginner to Python and trying to get my head around
I am a beginner of Python. I am trying now figuring out why the
I'm trying to get simple code working, unfortunately I'm a python beginner. My script
I am a beginner with Python and trying few programs. I have something like
Python beginner >>> import oauth2 Traceback (most recent call last): File , line 1,
I'm a beginner programmer and my first language is Python 2.7, I'm trying to
I am trying to set up Python on Windows 7. I haven't used this
I am a python beginner and I am trying to average two NumPy 2D

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.