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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:13:54+00:00 2026-06-13T04:13:54+00:00

I am trying to solve a more generic problem similar to the following. In

  • 0

I am trying to solve a more generic problem similar to the following. In the following, I get vow_array, which indicates the presence of a subset of vowels in some text, say, sent to my program. I need to print each vowel’s presence as 0 or 1.

ch_a = 0
ch_e = 0
ch_i = 0
ch_o = 0
ch_u = 0

# vow_array is generated at runtime; here is an example
vow_array = ['a', 'o', 'u']

if 'a' in vow_array:
    ch_a = ch_a + 1
if 'e' in vow_array:
    ch_e = ch_e + 1
if 'i' in vow_array:
    ch_i = ch_i + 1
if 'o' in vow_array:
    ch_o = ch_o + 1
if 'u' in vow_array:
    ch_u = ch_u + 1

print ch_a, ch_e, ch_i, ch_o, ch_u

I think this code is too long and prone to errors. Is there a more compact way of writing this? Also, say if I had to do this for all ‘letters’ in the alphabet I don’t want to have to repeat the code.

  • 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-13T04:13:55+00:00Added an answer on June 13, 2026 at 4:13 am

    Definitely.

    If you ever have variables with the same prefix (ch_a, ch_e, …), you need to use a dictionary or a list to group them:

    vowels = {
        'a': 0,
        'e': 0,
        'i': 0,
        'o': 0,
        'u': 0
    }
    
    vowel_array = ['a', 'o', 'u']
    
    for vowel in vowels.keys():
        if vowel in vowel_array:
            vowels[vowel] += 1
    
    print vowels
    

    A more Pythonic solution would be something like this:

    >>> from collections import Counter
    >>>
    >>> letters = 'i am a sentence'
    >>> Counter(letters)
    Counter({' ': 3, 'e': 3, 'a': 2, 'n': 2, 'c': 1, 'i': 1, 'm': 1, 's': 1, 't': 1})
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to solve a problem where I have some classes in which I
I'm trying to solve the following problem in Redis. I have a list that
I am trying to solve the following problem with Puppet: I have multiple nodes.
I run into some problems when trying to solve a problem I had with
I am trying to solve the following problem. Assume I have a HTML file
since I'm trying to solve this problem for some days, I thought about giving
For some time I have been trying to solve fairly common problem consisting of
I am trying to solve a tricky problem. I am creating a project, which
I'm trying to solve the following real-life problem you might have encountered yourselves: You
I am trying to solve a problem about the following code: <div contenteditable=true> <label

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.