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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:39:16+00:00 2026-06-08T22:39:16+00:00

I have dictionary of about 20,000 objects The key is a string representation of

  • 0

I have dictionary of about 20,000 objects The key is a string representation of the object, and the value is the object itself. Each object has attributes self.length and self.rate. self.rate is calculated as 1.5E-8*self.length.

I need to select, with replacement, a pre-determined number (we’ll say 500 for this example) of items from this dict based on their rate. objects with a lower rate will be less likely to be chosen and objects with a higher rate, more likely.

The way I thought I could do this is very slow.

In a while loop, while the number of selected objects is less than the number of required selections, I generate a random number between 0, and the length of the dict and choose that element. Then I generate another random number and if the random number is less than the rate of the chosen object in the list, that gets added to the selected objects. This seemed fine at first but now I am realising it’s much too slow. Does anyone have suggestions on how to do this faster?

Some code:
The class definition for the object

from numpy import random
class object():
    def __init__(self, length):
        self.length  = length
        self.rate = (1.15E-8*self.length)

    def select(self):
        x = random.uniform(0,1)
        if(x<self.rate):
            return True
        else:
            return False

And the function (in another module) that does the rest:

def select_random(object_dict,maxselect):
    nselect = 0
    object_names = object_dict.keys()
    selected_objects = []
    while(nselect < maxselect):
        x = random.randint(0,len(object_dict))
        if(object_dict[object_names[x]].select()):
            nselect +=1
            selected_objects.append(object_names[x])
    return(selected_objects)

I think what is making it really slow is that probability of each object being chosen is so small that there needs to many iterations before even one object is chosen let alone 500 or possibly more.

Distribution of lengths:

Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
     51     822    1311    1770    2112  103000 
  • 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-08T22:39:18+00:00Added an answer on June 8, 2026 at 10:39 pm

    Try this:

    import numpy as np    # requires NumPy 1.7 (!)
    
    def select_random(object_dict, n):
        keys = object_dict.keys()
        rate = np.array([x.rate for x in keys])
        prob = rate / rate.sum()
        return np.random.choice(keys, size=n, replace=True, p=prob)
    

    (Documentation)

    P.S., it’s a bad idea to call a class object, since that’s also the name of the built-in universal base class.

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

Sidebar

Related Questions

I have a dictionary of about 50,000 words; each word has-many synonyms, antonyms, etc.
I have about 20,000 NSDictionary s. Each dictionary contains three NSString s. The dictionaries
I have a question about Javascript's dictionary. I have a dictionary in which key-value
Does VBA have dictionary structure? Like key<>value array?
I have a Dictionary where the key and value are both strings. It's possible
I have a dictionary, user_dict, and create a list for each user value, this
I have information about a bunch of different items. Each item has its own
Another small question about STL: i have Dictionary: map <string,vector <Wordy> > Dictionary; using
I have a Dictionary<string,int> that has the potential to contain upwards of 10+ million
So, I have a dictionary-file with 70,000 lines which are ordered alphabetically. Each line

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.