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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:49:50+00:00 2026-05-29T08:49:50+00:00

I would like to split a string according to the title in a single

  • 0

I would like to split a string according to the title in a single call. I’m looking for a simple syntax using list comprehension, but i don’t got it yet:

s = "123456"

And the result would be:

["12", "34", "56"]

What i don’t want:

re.split('(?i)([0-9a-f]{2})', s)
s[0:2], s[2:4], s[4:6]
[s[i*2:i*2+2] for i in len(s) / 2]

Edit:

Ok, i wanted to parse a hex RGB[A] color (and possible other color/component format), to extract all the component.
It seem that the fastest approach would be the last from sven-marnach:

  1. sven-marnach xrange: 0.883 usec per loop

    python -m timeit -s 's="aabbcc";' '[int(s[i:i+2], 16) / 255. for i in xrange(0, len(s), 2)]'
    
  2. pair/iter: 1.38 usec per loop

    python -m timeit -s 's="aabbcc"' '["%c%c" % pair for pair in zip(* 2 * [iter(s)])]'
    
  3. Regex: 2.55 usec per loop

    python -m timeit -s 'import re; s="aabbcc"; c=re.compile("(?i)([0-9a-f]{2})"); 
    split=re.split' '[int(x, 16) / 255. for x in split(c, s) if x != ""]'
    
  • 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-29T08:49:51+00:00Added an answer on May 29, 2026 at 8:49 am

    Reading through the comments, it turns out the actual question is: What is the fastest way to parse a color definition string in hexadecimal RRGGBBAA format. Here are some options:

    def rgba1(s, unpack=struct.unpack):
        return unpack("BBBB", s.decode("hex"))
    
    def rgba2(s, int=int, xrange=xrange):
        return [int(s[i:i+2], 16) for i in xrange(0, 8, 2)]
    
    def rgba3(s, int=int, xrange=xrange):
        x = int(s, 16)
        return [(x >> i) & 255 for i in xrange(0, 32, 8)]
    

    As I expected, the first version turns out to be fastest:

    In [6]: timeit rgba1("aabbccdd")
    1000000 loops, best of 3: 1.44 us per loop
    
    In [7]: timeit rgba2("aabbccdd")
    100000 loops, best of 3: 2.43 us per loop
    
    In [8]: timeit rgba3("aabbccdd")
    100000 loops, best of 3: 2.44 us per loop
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to split a string into an array according to a regular
I would like to split a string into a String[] using a String as
I would like to split this string by a word data:: not by single
Would like to get a list of advantages and disadvantages of using Stored Procedures.
Suppose I have a string of 123456789 . I would like to split this
I have a string a and I would like to split it in half
I have an IEnumerable<string > which I would like to split into groups of
I would like to split the example string: ~Peter~Lois~Chris~Meg~Stewie on the character ~ and
I have a large string text file, I would like to split the string
I'd like to split a string using one or more separator characters. E.g. a

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.