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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:16:37+00:00 2026-06-12T07:16:37+00:00

Possible Duplicate: how to get the number of occurrences of each character using python

  • 0

Possible Duplicate:
how to get the number of occurrences of each character using python

What is the best way to obtain the count of each character in a string and store it(I’m using a dictionary for this – can this choice make a big difference?)? A couple of ways that I thought of:

1.

for character in string:
    if character in characterCountsDict:
        characterCountsDict[character] += 1
    else:
        characterCountsDict[character] = 1

2.

character = 0
while character < 127:
    characterCountsDict[str(unichr(character))] = string.count(str(unichr(character))
    character += 1

I think the second method is better…
But is either of them good?
Is there a much better way to do this?

  • 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-12T07:16:39+00:00Added an answer on June 12, 2026 at 7:16 am

    If you’re interested in the most efficient way, it appears to be like this:

    from collections import defaultdict
    
    def count_chars(s):
        res = defaultdict(int)
        for char in s:
            res[char] += 1
        return res
    

    Timings:

    from collections import Counter, defaultdict
    
    def test_counter(s):
        return Counter(s)
    
    def test_get(s):
        res = {}
        for char in s:
            res[char] = res.get(char, 0) + 1
        return res
    
    def test_in(s):
        res = {}
        for char in s:
            if char in res:
                res[char] += 1
            else:
                res[char] = 1
        return res
    
    def test_defaultdict(s):
        res = defaultdict(int)
        for char in s:
            res[char] += 1
        return res
    
    
    s = open('/usr/share/dict/words').read()
    #eof
    
    import timeit
    
    test = lambda f: timeit.timeit(f + '(s)', setup, number=10)
    setup = open(__file__).read().split("#eof")[0]
    results = ['%.4f %s' % (test(f), f) for f in dir() if f.startswith('test_')]
    print  '\n'.join(sorted(results))
    

    Results:

    0.8053 test_defaultdict
    1.3628 test_in
    1.6773 test_get
    2.3877 test_counter
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Facebook Profile Subscriber Count Is there any way to get number of
Possible Duplicate: How to get code point number for a given character in a
Possible Duplicate: Get variable name. javascript “reflection” Is there a way to know the
Possible Duplicate: Get file name from URI string in C# How to extract file
Possible Duplicate: Get the resolution of a jpeg image using C# and the .NET
Possible Duplicate: Get the IP Address of local computer Obtaining local IP address using
Possible Duplicate: Determine Number of Pages in a PDF File using C# (.NET 2.0)
Possible Duplicate: Get Last Day of the Month in Python How can I get
Possible Duplicate: Any chance to get unique records using Linq (C#)? using System; using
Possible Duplicate: Count bytes in textarea using javascript Hello everyone. I am trying to

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.