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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:21:35+00:00 2026-05-28T02:21:35+00:00

I would like to do something a bit tricky and I’m not sure the

  • 0

I would like to do something a bit tricky and I’m not sure the best way to go about it.

I have a two dimensional array that is in the form a nested list. Each “Row” in the list has the following structure:

['171000', 'Thing..Mega~Corporate~Thing..Mid~Dairy~Thing..Micro~Cheese', 'Cheese', '0.012174']

What I would like to do is loop through the entire array, row by row, and create a new row that splits the string in the second position into three new strings in the list so the result is as follows:

['171000', 'Mega~Corporate', 'Mid~Dairy', 'Micro~Cheese', 'Cheese', '0.012174']

A couple things I have to mention. In the above case, I split the string into three sub strings, Mega, Mid, Micro. Not every string will have a “Mid” and a “Micro” portion of it. Some of these strings could simply be shorter versions of the same format:

'Thing..Mega~Corporate'

For this case, I’d like to insert a placeholder to preserve the position of each element in the array (so that the numbers in the final position always have the same index).

The array I’m working with is rather large, upwards of 100k rows. Any help would be greatly appreciated as I’m really struggling with this one.

  • 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-28T02:21:36+00:00Added an answer on May 28, 2026 at 2:21 am

    If Thing.. represents an arbitrary text (not a literal data in the string):

    import re
    
    def explode(s, keywords):
        for k in keywords:
            m = re.search(r'(%s~[^~]*)(?:~|$)' % (re.escape(k),), s)
            yield m and m.group(1)
    
    for row in lst:
        row[1:2] = explode(row[1], "Mega Mid Micro".split())
    

    Example

    import re
    from pprint import pprint
    
    def explode(s, keywords):
        for k in keywords:
            m = re.search(r'(%s~[^~]*)(?:~|$)' % (re.escape(k),), s)
            yield m and m.group(1)
    
    
    lst = [
     ['171000', 'Thing..Mega~Corporate~Thing..Mid~Dairy~Thing..Micro~Cheese', 'Cheese', '0.012174'],
     ['171000', 'Thing..Mega~Corporate', 'Cheese', '0.012174'],
    ]
    
    print("Before:")
    pprint(lst)
    
    for row in lst:
        row[1:2] = explode(row[1], "Mega Mid Micro".split())
    
    print("\nAfter:")
    pprint(lst)
    

    Output

    Before:
    [['171000',
      'Thing..Mega~Corporate~Thing..Mid~Dairy~Thing..Micro~Cheese',
      'Cheese',
      '0.012174'],
     ['171000', 'Thing..Mega~Corporate', 'Cheese', '0.012174']]
    
    After:
    [['171000',
      'Mega~Corporate',
      'Mid~Dairy',
      'Micro~Cheese',
      'Cheese',
      '0.012174'],
     ['171000', 'Mega~Corporate', None, None, 'Cheese', '0.012174']]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing something that is a bit like Facebook's shared link preview. I would
I have two matrices (of approximately 300 x 100) and I would like to
I would like something that I can use as follows var msg = new
Does anyone know of a good alternative to cron? I would like something that
I would like to use something like CLR Profiles on .Net 2.0 to see
I would like display something more meaningful that animated gif while users upload file
I would like to do something like <test:di id=someService /`> <% someService.methodCall(); %> where
I would like to do something like the following but can't seem to get
I would like to do something like this: a rotating cube on a form.
I would like to do something like the following: def add(a, b): #some code

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.