Вы находитесь на странице: 1из 5

Search on Mathematica

Log In Sign Up

Mathematica
Questions

Tags

Users

Badges

Unanswered

Ask Question
_ Mathematica Stack Exchange is a question and answer site for users of Wolfram
Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
Anybody can ask a question Anybody can answer The best answers are voted up and
rise to the top
Merging one element into a two-element list

up vote
5
down vote
favorite
I am trying to find how can do this really quick:

list = {{a, b}, c, {d, e}, f, {g, h}, i}

Desired output List is:

{{a,b,c},{d,e,f},{g,h,i}}

Looks like simple but it takes me plenty of time to think... sorry if this one has
been asked before, I didn't find any answer yet.

list-manipulation
shareimprove this question
edited yesterday

kglr
124k6138303
asked yesterday

leon365
385

BTW, you cannot set values to List. Alexander Zeng yesterday

Yes, that's a typo, thanks for pointing it out! leon365 14 hours ago
add a comment
4 Answers
active oldest votes
up vote
11
down vote
Also:

Append @@@ Partition[list, 2]


ArrayReshape[list, {3, 3}]
{## & @@ #, #2} & @@@ Partition[list, 2]
all give

{{a, b, c}, {d, e, f}, {g, h, i}}


shareimprove this answer
edited 21 hours ago
answered yesterday

kglr
124k6138303
1
Out of these 3 methods, I'm guessing that ArrayReshape would be the most efficient,
right? Sjoerd Smit 17 hours ago

Thank you! Great answers! leon365 14 hours ago


add a comment
up vote
6
down vote
If the input list is consistently dimensioned, then this would work:

list = {{a, b}, c, {d, e}, f, {g, h}, i};


Partition[ Flatten[list, 1], 3]
I hope that this gets you what you need.

shareimprove this answer


answered yesterday

Stephen Wilkus
1065

yes, the pattern is consistent. Thanks for your help, this is work! leon365
yesterday
1
Partition[Flatten[list], 3] is enogh yode 19 hours ago
add a comment
up vote
5
down vote
BlockMap[Flatten, list, 2]
Of course, the BlockMap also can be Developer`PartitionMap

shareimprove this answer


edited 19 hours ago
answered 20 hours ago

yode
8,86922189

This should be the most concise one. Alexander Zeng 19 hours ago

Thank you, sir! leon365 14 hours ago


add a comment
up vote
2
down vote
Method I

Step by step

Cases[list, _List]
List /@ Complement[list, %]
Join[%%, %, 2]
give respectively

{{a, b}, {d, e}, {g, h}}

{{c}, {f}, {i}}

{{a, b, c}, {d, e, f}, {g, h, i}}


Method II

Flatten /@ Transpose @ GatherBy[list, Head]


shareimprove this answer
edited yesterday
answered yesterday

Alexander Zeng
540213

Wow, nice illustration! leon365 14 hours ago

@leon365 Glad to be helpful. :) Alexander Zeng 14 hours ago


add a comment
Your Answer

Sign up or log in

Sign up using Google


Sign up using Facebook
Sign up using Email and Password

Post as a guest

Name

Email

required, but never shown


Post Your Answer
By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged list-manipulation
or ask your own question.

asked

yesterday
viewed

338 times

active

today

BLOG
How Were Designing Channels
Animated Mathematica functions
8 votes comment stats
Related

15
MapThread with non-rectangular lists
0
How do I perform the riffle function backward?
5
Why SetDelayed won't work
1
Sort rows in a matrix with a vector
0
Delete row which contains specifc elements
2
General way to permute rows in a matrix (tensor)
2
list of items and group of alternative items
0
Evaluate in Manipulate
6
Elegant way for element-wise application of a list of functions
0
Print to video permutations
Hot Network Questions

The Missing Number Revised


My monitor's power button is broken - how do I black out the display without
unplugging it?
In the Winnie-The-Pooh universe, are Heffalumps real?
Should I buy this (gently) used, 15 year old, helmet?
Girlfriend's nosy mother keeps expressing that she'll show up unannounced
How does CryptoKitties track time?
Under what circumstances will the executable first found in the path not be used
looking for antonym of plutocracy
How can I convince my girlfriend not to worry about gifts I give her?
Password hash that can be upgraded without plaintext password
Movie where protagonist time-travels to dystopian future when people become
mentally degraded
Why didn't we detect that exoplanet before?
Story Id: Drunk engineer creates inertia-winder from spare parts in his garage
Selecting Multiple Layers with ArcPy?
What is actually moving in an endless runner?
How to stop being The Oracle
Is it important not scratch copper wire when removing insulation?
When would a Mormon choose to go to the temple instead of the church?
Accidentally addressed professor as Mrs instead of Ms
Is public Wi-Fi a threat nowadays?
The bounded halting problem is decidable. Why doesn't this conflict with Rice's
theorem?
Handling someone else's problem that has been dumped on my desk
Happy Birthday, Finland!
Loop through servers and run command
question feed
MATHEMATICA

Tour
Help
Chat
Contact
Feedback
Mobile
COMPANY

Stack Overflow
Stack Overflow Business
Developer Jobs
About
Press
Legal
Privacy Policy
STACK EXCHANGE
NETWORK

Technology
Life / Arts
Culture / Recreation
Science
Other
Blog Facebook Twitter LinkedIn
site design / logo 2017 Stack Exchange Inc; user contributions licensed under cc
by-sa 3.0 with attribution required. rev 2017.12.5.28025
Mathematica is a registered trademark of Wolfram Research, Inc. While the mark is
used herein with the limited permission of Wolfram Research, Stack Exchange and
this site disclaim all affiliation therewith.

Вам также может понравиться