WOLFRAM COMMUNITY
Connect with users of Wolfram technologies to learn, solve problems and share ideas
Join
Sign In
Dashboard
Groups
People
Group Abstract
Message Boards
Answer
(
Unmark
)
Mark as an Answer
WOLFRAM COMMUNITY
Dashboard
Groups
People
1
|
17.8K Views
|
15 Replies
|
11 Total Likes
View groups...
Follow this post
Share
Share this post:
GROUPS:
Mathematica
Wolfram Language
Flattening a list by preserving the innermost level
Matthias Odisio
Matthias Odisio, Thermo Fisher Scientific
Posted
13 years ago
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
Reply
|
Flag
15 Replies
Sort By:
Replies
Likes
Recent
0
Hans Milton
Hans Milton
Posted
4 years ago
POSTED BY:
Hans Milton
Reply
|
Flag
0
Raymond Low
Raymond Low
Posted
4 years ago
POSTED BY:
Raymond Low
Reply
|
Flag
0
Rohit Namjoshi
Rohit Namjoshi
Posted
4 years ago
POSTED BY:
Rohit Namjoshi
Reply
|
Flag
0
Rohit Namjoshi
Rohit Namjoshi
Posted
4 years ago
POSTED BY:
Rohit Namjoshi
Reply
|
Flag
0
Raymond Low
Raymond Low
Posted
4 years ago
POSTED BY:
Raymond Low
Reply
|
Flag
0
Raymond Low
Raymond Low
Posted
4 years ago
POSTED BY:
Raymond Low
Reply
|
Flag
1
Rohit Namjoshi
Rohit Namjoshi
Posted
4 years ago
POSTED BY:
Rohit Namjoshi
Reply
|
Flag
1
Rohit Namjoshi
Rohit Namjoshi
Posted
4 years ago
POSTED BY:
Rohit Namjoshi
Reply
|
Flag
0
Raymond Low
Raymond Low
Posted
4 years ago
POSTED BY:
Raymond Low
Reply
|
Flag
3
Suba Thomas
Suba Thomas, Wolfram Research Inc.
Posted
13 years ago
Maybe
In[205]:= Level[list, {-2}]
Out[205]= {{1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10}, {11, 12}}
POSTED BY:
Suba Thomas
Reply
|
Flag
1
Oleksandr Pavlyk
Oleksandr Pavlyk, Wolfram Research
Posted
13 years ago
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
Reply
|
Flag
1
Andrew Moylan
Andrew Moylan, Iteris, Inc.
Posted
13 years ago
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
Reply
|
Flag
1
Matthias Odisio
Matthias Odisio, Thermo Fisher Scientific
Posted
13 years ago
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
Reply
|
Flag
1
Paritosh Mokhasi
Paritosh Mokhasi, Wolfram Research Inc.
Posted
13 years ago
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
Reply
|
Flag
1
W. Craig Carter
W. Craig Carter, MIT
Posted
13 years ago
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
|
Flag
Reply to this discussion
in reply to
Add Notebook
Community posts can be styled and formatted using the
Markdown syntax
.
Tag limit exceeded
Note: Only the first five people you tag will receive an email notification; the other tagged names will appear as links to their profiles.
Publish anyway
Cancel
Reply Preview
Attachments
Remove
Add a file to this post
Follow this discussion
or
Discard
Be respectful. Review our
Community Guidelines
to understand your role and responsibilities.
Community Terms of Use
Feedback