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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:34:13+00:00 2026-06-18T15:34:13+00:00

I am writing a function to select randomly elements stored in a dictionary: import

  • 0

I am writing a function to select randomly elements stored in a dictionary:

import random
from liblas import file as lasfile
from collections import defaultdict

def point_random_selection(list,k):
    try:
        sample_point = random.sample(list,k)
    except ValueError:
        sample_point = list
    return(sample_point)

def world2Pixel_Id(x,y,X_Min,Y_Max,xDist,yDist):
    col = int((x - X_Min)/xDist)
    row = int((Y_Max - y)/yDist)
    return("{0}_{1}".format(col,row))

def point_GridGroups(inFile,X_Min,Y_Max,xDist,yDist):
    Groups = defaultdict(list)
    for p in lasfile.File(inFile,None,'r'):
        id = world2Pixel_Id(p.x,p.y,X_Min,Y_Max,xDist,yDist)
        Groups[id].append(p)
    return(Groups)

where k is the number of element to select. Groups is the dictionary

file_out = lasfile.File("outPut",mode='w',header= h)
for m in Groups.iteritems():
   # select k point for each dictionary key 
   point_selected = point_random_selection(m[1],k)
   for l in xrange(len(point_selected)):
     # save the data 
     file_out.write(point_selected[l])
file_out.close()

My problem is that this approach is extremely slow (for file of ~800 Mb around 4 days)

  • 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-18T15:34:14+00:00Added an answer on June 18, 2026 at 3:34 pm

    You could try and update your samples as you read the coordinates. This at least saves you from having to store everything in memory before running your sample. This is not guaranteed to make things faster.

    The following is based off of BlkKnght’s excellent answer to build a random sample from file input without retaining all the lines. This just expanded it to keep multiple samples instead.

    import random
    from liblas import file as lasfile
    from collections import defaultdict
    
    
    def world2Pixel_Id(x, y, X_Min, Y_Max, xDist, yDist):
        col = int((x - X_Min) / xDist)
        row = int((Y_Max - y) / yDist)
        return (col, row)
    
    def random_grouped_samples(infile, n, X_Min, Y_Max, xDist, yDist):
        """Select up to n points *per group* from infile"""
    
        groupcounts = defaultdict(int)
        samples = defaultdict(list)
    
        for p in lasfile.File(inFile, None, 'r'):
            id = world2Pixel_Id(p.x, p.y, X_Min, Y_Max, xDist, yDist)
            i = groupcounts[id]
            r = random.randint(0, i)
    
            if r < n:
                if i < n:
                    samples[id].insert(r, p)  # add first n items in random order
                else:
                    samples[id][r] = p  # at a decreasing rate, replace random items
    
            groupcounts[id] += 1
    
        return samples
    

    The above function takes inFile and your boundary coordinates, as well as the sample size n, and returns grouped samples that have at most n items in each group, picked uniformly.

    Because all you use the id for is as a group key, I reduced it to only calculating the col, row tuple, there is no need to make it a string.

    You can write these out to a file with:

    file_out = lasfile.File("outPut",mode='w',header= h)
    
    for group in samples.itervalues():
        for p in group:
            file_out.write(p)
    
    file_out.close()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a simple function for downloading a certain file, from the server,
I'm looking for a way to randomly select a file from a tree of
I am writing function that involve other function from base R with a lot
I'm currently writing a function what would create a zip file, which will be
I am writing a function to extract decimals from a number. Ignore the exception
I'm writing a function to call a stored procedure given an IDictionary of arguments.
I'm writing a Function that pulls Records from a DataBase using LINQ to get
I find myself writing code that looks something like this: $(document).ready(function(){ updateStuff(); $('select').change(function(){ updateStuff();
i am writing a c++ function that will parse .xml file. i Used system(grep);
I am messing up somewhere when writing a function to execute stored procedure. I

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.