Group Abstract Group Abstract

Message Boards Message Boards

0
|
6.8K Views
|
8 Replies
|
10 Total Likes
View groups...
Share
Share this post:

help cleaning up a list

Posted 12 years ago
POSTED BY: Michael Marino
8 Replies

Great!

POSTED BY: David Reiss
Posted 12 years ago

Of course! I feel silly for not realizing it sooner. TableForm had me thinking that the data was in columns when rows where the more accurate way to think about it. Transposing the original array before using the DeleteCases function has everything working perfectly now. Thanks again David for all your help!

Chris, that is some useful information that I will definitely use in the future.

Best regards,

Mike.

POSTED BY: Michael Marino
POSTED BY: David Reiss

Hmmmm... it's hard to say without an example. It is possible that the import from the text file is creating a data structure that is somewhat different from what you are expecting. Does the issue happen with all 3 approaches that I suggested? And, if so, does it happen in the same way? How large is the file that shows the problem? If it is email-able (less than 5 meg) then email it to the address that is posted on the "contact" section of my website (found from clicking on my name here).

By the way, the reason why you may have thought that the three dashes were not strings is that string characters (the quotation marks) are by default suppressed in output cells. So if you execute this in an input cell

"I am a string"

The output cell will look like this

I am a string
POSTED BY: David Reiss
Posted 12 years ago

Thanks Guys, I read up on DeleteCases and using the examples provide above was able to make some headway. David, you were correct about the strings and I apologize for that oversight Unfortunately the function seems to be reorganizing the data in the sublists in an unusal way. If the first sublist contains 50 items, and a sublist further along contains 150, it is taking items 51-150 from the latter list and appending them to the end of the first sublist. The net result is a series of lists that decrease in length and have no dashes, but also bear little similarity to the original data structure. I can't seem to reproduce this with a smaller example and several attempts at attaching the text file to this post have failed. I'm hoping that my admittedly poor description might ring a bell with someone who can point me in the right direction. I really appreciate the help. I'm trying to crack this myself, but I have to admit that I am at the limits of my current understanding. Thanks, Mike.

POSTED BY: Michael Marino

Is the list that you obtain when it is imported into Mathematica this,

{{1,2,3,---},{4,5,---},{---,---,---},{7,8,9,10,---}}

or is it this:

{{1,2,3,"---"},{4,5,"---"},{"---","---","---"},{7,8,9,10,"---"}}

I.e., are the dashes imported as a string of dashes? Without them being strings, the expressions (of 3 dashes in a row) are not syntactically valid. So I will assume that they appear as strings. If so then the following will work for you:

In[1]:= test = {{1, 2, 3, "---"}, {4, 5, "---"}, {"---", "---", "---"}, {7, 8, 9, 10, "---"}}

In[2]:= DeleteCases[test /. "---" -> Sequence[], {}, Infinity]

Out[2]= {{1, 2, 3}, {4, 5}, {7, 8, 9, 10}}

Another way to do it might be

In[3]:= DeleteCases[DeleteCases[test, "---", Infinity], {}, Infinity]

Out[3]= {{1, 2, 3}, {4, 5}, {7, 8, 9, 10}}

And here's another....

In[4]:= (test /. "---" -> Sequence[]) /. {} -> Sequence[]

Out[4]= {{1, 2, 3}, {4, 5}, {7, 8, 9, 10}}
POSTED BY: David Reiss

Please see the function DeleteCases:

DeleteCases[{1, 2, 3, "---"}, "---"]
POSTED BY: Sean Clarke
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard