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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:26:57+00:00 2026-05-27T05:26:57+00:00

I am trying to port a cgi script using pythonic style of coding. sequence

  • 0

I am trying to port a cgi script using pythonic style of coding.

sequence = "aaaabbababbbbabbabb"
res = sequence.split("a") + sequence.split("b")
res = [l for l in res if l]

The result is

>>> res
['bb', 'b', 'bbbb', 'bb', 'bb', 'aaaa', 'a', 'a', 'a', 'a']

This was ~100loc in C. Now i want to count the items with the same length in the res list efficiently. For example here res contains 5 elements with length 1, 3 elements with length 2 and 2 elements with length 4.

The problem is that the sequence string can be very big.

  • 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-27T05:26:58+00:00Added an answer on May 27, 2026 at 5:26 am

    The easiest way to generate a histogram of string lengths given a list of strings is to use collections.Counter:

    >>> from collections import Counter
    >>> a = ["a", "b", "aaa", "bb", "aa", "bbb", "", "a", "b"]
    >>> Counter(map(len, a))
    Counter({1: 4, 2: 2, 3: 2, 0: 1})
    

    Edit: There is also a better way to find runs of equal characters, namely itertools.groupby():

    >>> sequence = "aaaabbababbbbabbabb"
    >>> Counter(len(list(it)) for k, it in groupby(sequence))
    Counter({1: 5, 2: 3, 4: 2})
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to port code over to compile using Microchip's C18 compiler for a
I am trying to run a simple cgi script after configuring my server. My
I'm trying to port a shell script to the much more readable python version.
I'm trying to port an application I wrote in java to javascript (actually using
I'm trying to port a PHP script to Ruby and until now I only
I'm currently trying to port a SIP stack library (pjSIP) to the PSP Console
I'm trying to port an application to Mono, however Mono doesn't support Application Settings.
i'm trying to port some Java stuff to C#. I'm just wondering if the
I'm trying to port a PHP site developed by another coder (who is no
I'm currently trying to port an app from asp.net to php, however I just

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.