1

having trouble with mathematica using nested lists. I want to save a list like this:

l1={{a1,{a,b,c,d,e}},{a2,{f,g,h,i}}}

So, the nested components do not have an identical dimension. However, whenever I save the file (as csv or xls) and reimport it seems that the inner lists (like l1[[1,2]] is transformed into a string which I would like to bypass somehow.

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
noclue11
  • 13
  • 2
  • 2
    If your aim is to re-import into Mathematica, use a format that can store arbitrary Mathematica expressions. This includes the "WDX", "Package" and "MX" Export formats. Some prefer using Compress and exporting the result as a "String". Note that "MX" is not cross-version-compatible and not cross platform before v10. – Szabolcs Apr 15 '15 at 00:05
  • 1
    "CSV stores tabular data." More specifically, it stores ragged-right 2D arrays. XLS stores a set of sparse 2D arrays and some metadata. They are simply not capable of encoding nested lists of arbitrary depth. – 2012rcampion Apr 15 '15 at 03:14
  • thanks a lot for the clarification with the .csv and .xls-files. The MX-package works fine, too! – noclue11 Apr 15 '15 at 13:38

1 Answers1

1

Having

l1 = {{a1, {a, b, c, d, e}}, {a2, {f, g, h, i}}};

one exports using

Put[l1,"out.dat"]

and imports next with

Get["out.dat"]

If you don't SetDirectory, it will go to home directory and be then imported from there also.


See also this answer.

corey979
  • 23,947
  • 7
  • 58
  • 101
  • Does that really resolve the issue here? Essentially, I am not able to access the parts of my nested lists because some are simply converted to a string? – noclue11 Apr 14 '15 at 16:22
  • I updated the answer providing a working example. – corey979 Apr 14 '15 at 23:06