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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:34:05+00:00 2026-05-26T14:34:05+00:00

I am trying to write a program to draw a histogram of the lengths

  • 0

I am trying to write a program to draw a histogram of the lengths of words present in a list, so far I am up the stage of increasing the number of words present of a certain length by one every time the loop finds a word with that certain length, at the moment I have:

L = []
for i in range(L):
length = len(i)
for len(i) = 1:
    total1 = total1 + 1
for len(i) = 2:
    total2= total2 + 1
for len(i) = 3:
    total3 = total3 + 1
for len(i) = 4:
    total4 = total4 + 1
for len(i) = 5:
    total5 = total5 + 1

However clearly this is a stupid method as it involved naming each version of totaln, where in this case n would be up to 11, so my question is can I simply put

L = []
for i in range(L):
length = len(i)
for len(i) = n:
    totaln = totaln + 1

to cover all values of n, and then refer to for example total4 later? Or will the interpreter return an error as total4 isn’t explicitly defined?

As for the rest of the code I think I can work that bit out, it’s just this issue I am having trouble with, being very new to programming.

  • 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-26T14:34:06+00:00Added an answer on May 26, 2026 at 2:34 pm

    In Python 2.7 or above, you could use a Counter:

    from collections import Counter
    a = ["basically", "in", "Python", "I", "am", "trying", "to", "write",
         "a", "program", "to", "draw", "a", "histogram", "of", "the",
         "lengths", "of", "words", "present", "in", "a", "list"]
    print Counter(map(len, a))
    

    prints

    Counter({2: 7, 1: 4, 7: 3, 4: 2, 5: 2, 6: 2, 9: 2, 3: 1})
    

    which is a dictionary mapping word lengths to frequencies.

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

Sidebar

Related Questions

Im trying to write a program that forks and sends sort (linux) some words
Alright, so I'm trying to write a program for Android for a project for
I'm trying to write program calculating average of given numbers stored in an array.
I trying to write a program that navigates the local file system using a
Im trying to write a program to read a text file through args but
I am trying to write a program that takes an input of of n
I'm trying to write a program for school using the BigDecimal class. The program
I am trying to write simple program using ONLY for loop ( if then
I am trying to write a program that will output via coordinates. I am
I'm trying to write a program in C (on Linux) that loops until the

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.