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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T11:01:36+00:00 2026-05-19T11:01:36+00:00

The input list can be more than 1 million numbers. When I run the

  • 0

The input list can be more than 1 million numbers. When I run the following code with smaller ‘repeats’, its fine;

def sample(x):
    length = 1000000 
    new_array = random.sample((list(x)),length)
    return (new_array)

def repeat_sample(x):    
    i = 0
    repeats = 100
    list_of_samples = []
    for i in range(repeats):
       list_of_samples.append(sample(x))
    return(list_of_samples)

repeat_sample(large_array)

However, using high repeats such as the 100 above, results in MemoryError. Traceback is as follows;

Traceback (most recent call last):
  File "C:\Python31\rnd.py", line 221, in <module>
    STORED_REPEAT_SAMPLE = repeat_sample(STORED_ARRAY)
  File "C:\Python31\rnd.py", line 129, in repeat_sample
    list_of_samples.append(sample(x))
  File "C:\Python31\rnd.py", line 121, in sample
    new_array = random.sample((list(x)),length)
  File "C:\Python31\lib\random.py", line 309, in sample
    result = [None] * k
MemoryError

I am assuming I’m running out of memory. I do not know how to get around this problem.

Thank you for your time!

  • 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-05-19T11:01:36+00:00Added an answer on May 19, 2026 at 11:01 am

    Expanding on my comment:

    Let’s say the processing you do to each sample is calculate its mean.

    def mean(samplelists):
        means = []
        n = float(len(samplelists[0]))
        for sample in samplelists:
            mean = sum(sample)/n
            means.append(mean)
        return means
    
    calc_means(repeat_sample(large_array))
    

    This is going to make you sweat holding all those lists in memory. You can get it much lighter like this:

    def mean(sample, n):
        n = float(n)
        mean = sum(sample)/n
        return mean
    
    def sample(x):
        length = 1000000 
        new_array = random.sample(x, length)
        return new_array
    
    def repeat_means(x):    
        repeats = 100
        list_of_means = []
        for i in range(repeats):
            list_of_means.append(mean(sample(x)))
        return list_of_means    
    
    repeat_means(large_array)
    

    But that’s still not good enough… You can do it all with only ever constructing your list of results:

    import random
    
    def sampling_mean(population, k, times):
        # Part of this is lifted straight from random.py
        _int = int
        _random = random.random
    
        n = len(population)
        kf = float(k)
        result = []
    
        if not 0 <= k <= n:
            raise ValueError, "sample larger than population"
    
        for t in range(times):
            selected = set()
            sum_ = 0
            selected_add = selected.add
    
            for i in xrange(k):
                j = _int(_random() * n)
                while j in selected:
                    j = _int(_random() * n)
                selected_add(j)
                sum_ += population[j]
    
            mean = sum_/kf
            result.append(mean)
        return result
    
    sampling_mean(x, 1000000, 100)
    

    Now, can your algorithm be streamlined like this?

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

Sidebar

Related Questions

No related questions found

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.