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 8821343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:49:49+00:00 2026-06-14T05:49:49+00:00

I have a file in *.las format and i wish to count how many

  • 0

I have a file in *.las format and i wish to count how many points are Return1, Return2,…,Return5
I use a If…Else Statements, but i wish to understand if there is a way more elegant and code saving. Thanks in advance for help and tips.

    Return1 = 0
    Return2 = 0
    Return3 = 0
    Return4 = 0
    Return5 = 0
    count = 0
    for p in lasfile.File(inFile,None,'r'):
        count +=1
        if p.return_number == 1:
            Return1 += 1
        elif p.return_number == 2:
            Return2 += 1
        elif p.return_number == 3:
            Return3 += 1
        elif p.return_number == 4:
            Return4 += 1
        elif p.return_number == 5:
            Return5 += 1
  • 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-14T05:49:50+00:00Added an answer on June 14, 2026 at 5:49 am

    You can use a dict of counters (in this case it can also be a list). defaultdict is quite convenient:

    from collections import defaultdict
    returns = defaultdict(int)
    count = 0
    for p in lasfile.File(inFile, None, 'r'):
        count += 1
        returns[p.return_number] += 1
    

    Edit: I’m not sure if DSM is posting a separate answer with Counter examples, so I’m going to reply to the OP’s question here:

    @DSM: could i combine Counter? ex: if i wish to count p.return_number and p.scan_angle in the same time – Gianni

    Counter will count values in the iterable you give it. The iterable is supposed to be homogenic. For example, if scan_angles can take values of 1, 2, etc., then you can’t use the same Counter for both and get correct results. If the values don’t overlap, you can get correct results, but your code will be confusing and hard to understand. I’d suggest to use two Counter‘s: one for return_numbers and one for scan_angle‘s. You can use the same loop to populate both easily (and the same stands for defaultdict‘s), but if you want to compress creation and filling of two Counter‘s into one statement, you’ll need something like

    from collections import Counter
    rnc, sac = map(Counter, 
      zip(*((p.return_number, p.scan_angle) for p in lasfile.File(inFile,None,'r'))))
    

    Note that at least on Python 2 it will produce a list, so all data will be held in memory at once at some point.

    Yet, the much simpler ways to go are:

    • use two defaultdicts:

      returns, angles = defaultdict(int), defaultdict(int)
      for p in lasfile.File(inFile, None, 'r'):
           returns[p.return_number] += 1
           angles[p.scan_angle] += 1
           count += 1
      
    • or fill two Counters in two different reads of the file:

      fp = lasfile.File(inFile, None, 'r')
      returns = Counter(p.return_number for p in fp)
      fp = lasfile.File(inFile, None, 'r')
      angles = Counter(p.scan_angle for p in fp)
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have file in json format of world-countries I want to change it to
have file data of format 3.343445 1 3.54564 1 4.345535 1 2.453454 1 and
I have file like this which contains 5 columns. But some rows in my
I have file with this specific format: 0 2 4 0 1 A 0
I have file in format DOC (MS Word 97-2003) and I want to get
I have File Upload form which users use to upload certain files on the
I have file with multiple SQL statements in it to be executed. INSERT INTO
I have file with lines like so: 1 17 A G R:560:500:60:10.71%:1.6329E-19 Pass:1.0:276:0:57:0:1E0 15
I have file with name contact.php, inside I have form: <form method=post name=kontakt action=send_mail.php>
I have file locale/es/LC_MESSAGES/django.mo (and .po), ran makemessages and compilemessages. Definitely all messages are

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.