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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:37:32+00:00 2026-05-26T23:37:32+00:00

I am making a function which will make a list which includes a histogram

  • 0

I am making a function which will make a list which includes a histogram of a requested list and requested values to use as the values of the histogram. Values above the requested value are included last.

The program is working with a list that is sorted in ascending order numerically, but when a list that is not sorted is used as input, the program seems to discard random values and not evaluate in the same way.
the code:

def histogram(sample, binBoundaries):
    c=0
    if not binBoundaries:
        li = [len(sample)]
        return print(li)
    for x in sample:
        if x > binBoundaries[-1]: #if the value is greater than last bin
            c = c+1            #number of values greater increases
    for eachbin in binBoundaries: #for each bin
        dic[eachbin] = 0          #initial value = 0 to account for no number
        for x in sample:          #for each value wanted to calculate for
            if x <= eachbin:       #if the number falls into the bin
                dic[eachbin] += 1 #the number of values in the bin increases
            sample.remove(x)
    for i in dic:
        listofvalues.append(dic[i])
    listofvalues.append(c)
    print(listofvalues)

histogram([5, 4, 2, 3], [3])

this will result in an output of [1, 2], where the real output should be [2,2]

Is there something I’m just not seeing that is making the number not be calculated? Let me know where I have gone wrong if you could!

  • 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-26T23:37:33+00:00Added an answer on May 26, 2026 at 11:37 pm

    Your problem is that you are removing items from the list sample while you are iterating over it, this is a bad idea because it will result in skipping some elements.

    Try taking out the line sample.remove(x), you should get the expected result. If you really do need to remove elements from the input list you should refactor to make sure you still check every element in the list. One option is to iterate over the list in reverse using for x in reversed(sample).

    It also looks like you may be removing elements in the wrong place, sample.remove(x) looks like it should be inside of the if directly above it. Try the following code:

        ...
        for eachbin in binBoundaries: #for each bin
            dic[eachbin] = 0          #initial value = 0 to account for no number
            for x in reversed(sample):          #for each value wanted to calculate for
                if x <= eachbin:       #if the number falls into the bin
                    dic[eachbin] += 1 #the number of values in the bin increases
                    sample.remove(x)
        ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im making firefox extension. One function stores value in every tab, to use it
I'm making a function which will accept either an Unicode string or a bytes
I'm trying to make a Simple Nested List which will be some what similar
I am trying to make a removeAll() function, which will remove all elements of
Ok, I attempted at making my own search function which doesnt work very well.
I'm currently trying to make a button which will take the user back to
I am making a web page which will allow users to input and view
I have a function which I use to limit form inputs to numbers only,
I am making a registration table which will check if the cellphone number typed
I'm writing a utility function which will take a vector of elements (could be

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.