Group Abstract Group Abstract

Message Boards Message Boards

1
|
17.8K Views
|
15 Replies
|
11 Total Likes
View groups...
Share
Share this post:

Flattening a list by preserving the innermost level

Is there a better way to compute the following?
In[1]:= list = {{1, 2}, {{3, 4}, {5, 6}, {7, 8}, {9, 10}}, {{11, 12}}};
        Partition[Flatten[list], 2]
Out[2]= {{1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10}, {11, 12}}
POSTED BY: Matthias Odisio
15 Replies
Posted 4 years ago
POSTED BY: Hans Milton
Posted 4 years ago
POSTED BY: Raymond Low
Posted 4 years ago
POSTED BY: Rohit Namjoshi
Posted 4 years ago
POSTED BY: Rohit Namjoshi
Posted 4 years ago
POSTED BY: Raymond Low
Posted 4 years ago
POSTED BY: Raymond Low
Posted 4 years ago
POSTED BY: Rohit Namjoshi
Posted 4 years ago
POSTED BY: Rohit Namjoshi
Posted 4 years ago
POSTED BY: Raymond Low
Maybe
In[205]:= Level[list, {-2}]
Out[205]= {{1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10}, {11, 12}}
POSTED BY: Suba Thomas
Possibly
In[104]:= Cases[list, {__Integer}, Infinity]
Out[104]= {{1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10}, {11, 12}}
POSTED BY: Oleksandr Pavlyk
To clarify, by "better" do you mean "faster (with large ragged arrays of machine integers that are packed when possible)"?

Or something different?
POSTED BY: Andrew Moylan
For my very problem, speed is not an issue. I was hoping to discover
an elegant alternative way, perhaps using an arcane (for me)
syntactic form of Flatten, Replace, or other related functions.

Paritosh's solution expresses well that I would like to "flatten" as
much as possible starting from the the outermost braces and stopping
so the innermost List is untouched.
POSTED BY: Matthias Odisio
How about
In[2]:= list /. List[List[x__]] -> List[x]
Out[2]= {{1, 2}, {{3, 4}, {5, 6}, {7, 8}, {9, 10}}, {11, 12}}
POSTED BY: Paritosh Mokhasi
Hello Matthias,
I believe your method is more efficient than this alternative:
Cases[list, {_Integer, _Integer}, Infinity]
Which is a different way of thinking about it.
Craig
POSTED BY: W. Craig Carter
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard