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

  • Home
  • SEARCH
  • 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 3783056
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T11:10:13+00:00 2026-05-19T11:10:13+00:00

I have this array: >>> print raw_data [‘LEVEL 1’, ‘SUBJECT A’, ‘GROUP X’, ‘COMMENT

  • 0

I have this array:

>>> print raw_data
['LEVEL 1',
'SUBJECT A',
'GROUP X',
'COMMENT i',
'COMMENT ii',
'COMMENT iii',
'GROUP Y',
'COMMENT iv',
'COMMENT v',
'COMMENT vi',
'LEVEL 2',
'SUBJECT B',
'GROUP Z',
'COMMENT vii',
'COMMENT viii',
'COMMENT ix',
'SUBJECT C',
'GROUP X2',
'COMMENT x',
'COMMENT xi',
'COMMENT xii',
'COMMENT xiii',
'GROUP Y2',
'COMMENT xiv',
'COMMENT xv',
'COMMENT xvi']

Where the obvious hierarchy is:

  1. Level
    1. Subject
      1. Group
        1. Comments

My objective is to get the array as a denormalized array to be store on a database:

>>> print result
[
    ['LEVEL 1', 'SUBJECT A', 'GROUP X', 'COMMENT i'],
    ['LEVEL 1', 'SUBJECT A', 'GROUP X', 'COMMENT ii'],
    ['LEVEL 1', 'SUBJECT A', 'GROUP X', 'COMMENT iii'],
    ['LEVEL 1', 'SUBJECT A', 'GROUP Y', 'COMMENT iv'],
    ['LEVEL 1', 'SUBJECT A', 'GROUP Y', 'COMMENT v'],
    ['LEVEL 1', 'SUBJECT A', 'GROUP Y', 'COMMENT vi'],
    ['LEVEL 2', 'SUBJECT B', 'GROUP Z', 'COMMENT vi'],
    ['LEVEL 2', 'SUBJECT B', 'GROUP Z', 'COMMENT vii'],
    ['LEVEL 2', 'SUBJECT B', 'GROUP Z', 'COMMENT viii'],
    ['LEVEL 2', 'SUBJECT B', 'GROUP Z', 'COMMENT ix'],
    ['LEVEL 2', 'SUBJECT C', 'GROUP X1', 'COMMENT x'],
    ['LEVEL 2', 'SUBJECT C', 'GROUP X1', 'COMMENT xi'],
    ['LEVEL 2', 'SUBJECT C', 'GROUP X1', 'COMMENT xii'],
    ['LEVEL 2', 'SUBJECT C', 'GROUP X1', 'COMMENT xiii],'
    ['LEVEL 2', 'SUBJECT C', 'GROUP Y2', 'COMMENT xiv'],
    ['LEVEL 2', 'SUBJECT C', 'GROUP Y2', 'COMMENT xv'],
    ['LEVEL 2', 'SUBJECT C', 'GROUP Y2', 'COMMENT xi']
]

I was trying to solve this, but I am quite lost, I think this problem has to be usual, so I would like to know if someone has a efficient approach, this seems to be something like nested sets, but I don’t know a lot of this on python, getting the level is easy, but I am getting ” headaches” getting this further.

>>> def addlevel(a):
    if a.startswith('LEVEL'):
        return [1, a]
    elif a.startswith('SUBJECT'):
        return [2, a]
    elif a.startswith('GROUP'):
        return [3, a]
    elif a.startswith('COMMENT'):
        return [4, a]
>>> map(addlevel, raw_data)
[[1, 'LEVEL 1'],
 [2, 'SUBJECT A'],
 [3, 'GROUP X'],
 [4, 'COMMENT i'],
 [4, 'COMMENT ii'],
 [4, 'COMMENT iii'],
 [3, 'GROUP Y'],
 [4, 'COMMENT iv'],
 [4, 'COMMENT v'],
 [4, 'COMMENT vi'],
 [1, 'LEVEL 2'],
 [2, 'SUBJECT B'],
 [3, 'GROUP Z'],
 [4, 'COMMENT vii'],
 [4, 'COMMENT viii'],
 [4, 'COMMENT ix'],
 [2, 'SUBJECT C'],
 [3, 'GROUP X2'],
 [4, 'COMMENT x'],
 [4, 'COMMENT xi'],
 [4, 'COMMENT xii'],
 [4, 'COMMENT xiii'],
 [3, 'GROUP Y2'],
 [4, 'COMMENT xiv'],
 [4, 'COMMENT xv'],
 [4, 'COMMENT xvi']]

I would appreciate any clues !

  • 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-19T11:10:13+00:00Added an answer on May 19, 2026 at 11:10 am

    You could try something like this:

    raw_data = [ 'LEVEL 1', 'SUBJECT A', 'GROUP X', 'COMMENT i', 'COMMENT ii',
    'COMMENT iii', 'GROUP Y', 'COMMENT iv', 'COMMENT v', 'COMMENT vi', 'LEVEL 2',
    'SUBJECT B', 'GROUP Z', 'COMMENT vii', 'COMMENT viii', 'COMMENT ix', 
    'SUBJECT C', 'GROUP X2', 'COMMENT x', 'COMMENT xi', 'COMMENT xii', 
    'COMMENT xiii', 'GROUP Y2', 'COMMENT xiv', 'COMMENT xv', 'COMMENT xvi' ]
    
    level, subject, group, comment = '', '', '', ''
    
    result = []
    
    for item in raw_data:
    
        if item.startswith('COMMENT'): 
            comment = item
        elif item.startswith('GROUP'): 
            group = item
            comment = ''
        elif item.startswith('SUBJECT'): 
            subject = item
            group = ''
        elif item.startswith('LEVEL'): 
            level = item
            subject = ''
    
        if level and subject and group and comment:
            result.append([level, subject, group, comment])
    
    import pprint
    pprint.pprint(result)
    

    Which would yield:

    [['LEVEL 1', 'SUBJECT A', 'GROUP X', 'COMMENT i'],
     ['LEVEL 1', 'SUBJECT A', 'GROUP X', 'COMMENT ii'],
     ['LEVEL 1', 'SUBJECT A', 'GROUP X', 'COMMENT iii'],
     ['LEVEL 1', 'SUBJECT A', 'GROUP Y', 'COMMENT iv'],
     ['LEVEL 1', 'SUBJECT A', 'GROUP Y', 'COMMENT v'],
     ['LEVEL 1', 'SUBJECT A', 'GROUP Y', 'COMMENT vi'],
     ['LEVEL 2', 'SUBJECT B', 'GROUP Z', 'COMMENT vii'],
     ['LEVEL 2', 'SUBJECT B', 'GROUP Z', 'COMMENT viii'],
     ['LEVEL 2', 'SUBJECT B', 'GROUP Z', 'COMMENT ix'],
     ['LEVEL 2', 'SUBJECT C', 'GROUP X2', 'COMMENT x'],
     ['LEVEL 2', 'SUBJECT C', 'GROUP X2', 'COMMENT xi'],
     ['LEVEL 2', 'SUBJECT C', 'GROUP X2', 'COMMENT xii'],
     ['LEVEL 2', 'SUBJECT C', 'GROUP X2', 'COMMENT xiii'],
     ['LEVEL 2', 'SUBJECT C', 'GROUP Y2', 'COMMENT xiv'],
     ['LEVEL 2', 'SUBJECT C', 'GROUP Y2', 'COMMENT xv'],
     ['LEVEL 2', 'SUBJECT C', 'GROUP Y2', 'COMMENT xvi']]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array that looks like this. [{EntryId=>2, Field1=>National Life Group,DateCreated=>2010-07-30 11:00:14, CreatedBy=>tristanoneil},
I have this Array i wrote a function MostFreq that takes an array of
Lets say I have this array, int[] numbers = {1, 3, 4, 9, 2};
I have a class that contains an array. I want this array to be
Lets say I have an array like this: string [] Filelist = ... I
When you have an array like this: int foo[3][2][2]; and you make: int *bar
I need to move backwards through an array, so I have code like this:
I have code similar to this filtering entries in an Array of Objects: var
I have an array: data.Dealer.car[0] data.Dealer.car[1] data.Dealer.car[2] If I do this: alert(data.Dealer.car.length); delete data.Dealer.car[1];
I have CSV data loaded into a multidimensional array. In this way each row

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.