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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:47:02+00:00 2026-06-06T14:47:02+00:00

I have some python code that I wrote to convert a python list into

  • 0

I have some python code that I wrote to convert a python list into an XML element. It’s meant for interacting with LabVIEW, hence the weird XML array format. Anyways, here’s the code:

def pack(data):
  # create the result element
  result = xml.Element("Array")

  # report the dimensions
  ref = data
  while isinstance(ref, list):
    xml.SubElement(result, "Dimsize").text = str(len(ref))
    ref = ref[0]

  # flatten the data
  while isinstance(data[0], list):
    data = sum(data, [])

  # pack the data
  for d in data:
    result.append(pack_simple(d))

  # return the result
  return result

Now I need to write an unpack() method to convert the packed XML Array back into a python list. I can extract the array dimensions and data just fine:

def unpack(element):
  # retrieve the array dimensions and data
  lengths = []
  data = []
  for entry in element:
    if entry.text == "Dimsize":
      lengths.append(int(entry.text))

    else:
      data.append(unpack_simple(entry))

  # now what?

But I am not sure how to unflatten the array. What would be an efficient way to do that?

Edit: Here’s what the python list and corresponding XML looks like. Note: the arrays are n-dimensional.

data = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]

And then the XML version:

<Array>
  <Dimsize>2</Dimsize>
  <Dimsize>2</Dimsize>
  <Dimsize>2</Dimsize>
  <I32>
    <Name />
    <Val>1</Val>
  </I32>

  ... 2, 3, 4, etc.
</Array>

The actual format isn’t important though, I just don’t know how to unflatten the list from:

data = [1, 2, 3, 4, 5, 6, 7, 8]

back into:

data = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]

given:

lengths = [2, 2, 2]

Assume pack_simple() and unpack_simple() do the same as pack() and unpack() for the basic data types (int, long, string, boolean).

  • 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-06T14:47:04+00:00Added an answer on June 6, 2026 at 2:47 pm

    Try the following:

    from operator import mul
    
    def validate(array, sizes):
        if reduce(mul, sizes) != len(array):
            raise ValueError("Array dimension incompatible with desired sizes")
    
        return array, sizes
    
    def reshape(array, sizes):
        for s in sizes:
            array = [array[i:i + s] for i in range(0, len(array), s)]
    
        return array[0]
    
    data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
    length = [2, 2, 3]
    
    print reshape(validate(data, length))
    
    length = [2, 2, 2]
    
    print reshape(validate(data, length))
    

    Output being:

    [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]]
    Traceback:
       (...)
    ValueError: Array dimension incompatible with desired sizes
    

    An alternative is using numpy arrays. Note that for this simple task, numpy is a rather big dependency, though you will find that most (common) array related tasks/problems already have an implementation there:

    from numpy import array
    
    print array(data).reshape(*length)  # optionally add .tolist() to convert to list
    

    EDIT: Added data validation

    EDIT: Example using numpy arrays (thanks to J.F.Sebastian for the hint)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For my first foray into python, I wrote some parsing code that works as
I have some python code that currently performs expensive computation by performing the computation
I have some code in a python string that contains extraneous empty lines. I
I have a python ndarray temp in some code I'm reading that suffers this:
I have some python code for Google App Engine that responds with the string
I have heard of some compilers that convert code in one high level language
I have some python code which generates a 256^3 numpy array of data that
I have some python code that: Takes a BLOB from a database which is
I have a Django query and some Python code that I'm trying to optimize
I have some python code using shutil.copyfile: import os import shutil src='C:\Documents and Settings\user\Desktop\FilesPy'

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.