0
$\begingroup$
list1={{"a",{"a1","a2","a3","a4"}},(*{"b",{"b1"}},*){"c",{"c1","c2","c3","c4"}}}

list2={{"a",{"aa1","aa2","aa3"}},{"b",{"bb1","bb2","bb3","bb4"}},{"c",{"cc1","cc2","cc3"}}}

how to get this from above lists:

{{"a",{"a1","a2","a3","a4","aa1","aa2","aa3"}},{"b",{(*"b1",*)"bb1","bb2","bb
3","bb4"}},{"c",{"c1","c2","c3","c4","cc1","cc2","cc3"}}}

???

$\endgroup$
6
  • 1
    $\begingroup$ Welcome! A terse verbose description of what you want to achieve would be useful in addition. The more effort you invest in your question, the more likely you will get good answers. $\endgroup$
    – Yves Klett
    Commented Mar 19, 2016 at 6:59
  • $\begingroup$ someone asked me to solve this challenge. All I want is what should I do with first two lists to get the third list. $\endgroup$
    – Phenom
    Commented Mar 19, 2016 at 7:02
  • $\begingroup$ For example to get the first element of the resulting list all I need to do is take union(may be once or twice) of first element of both lists . $\endgroup$
    – Phenom
    Commented Mar 19, 2016 at 7:04
  • 1
    $\begingroup$ You will not be able to keep comments during manipulation unless you convert everything to strings. Comments will be ignored / stripped in interpretation. $\endgroup$
    – Yves Klett
    Commented Mar 19, 2016 at 9:00
  • 1
    $\begingroup$ According to wiki: In computer programming, a comment is a programmer-readable annotation in the source code of a computer program. They are added with the purpose of making the source code easier to understand, and are generally ignored by compilers and interpreters. For your purpose, you might need Style or the new-in-v10.4 Highlighted instead. $\endgroup$
    – xzczd
    Commented Mar 19, 2016 at 9:53

2 Answers 2

3
$\begingroup$

With GroupBy plus using Highlighted from @xzczd comment above.

list1 = {{"a", {"a1", "a2", "a3", "a4"}}, {"b", {Highlighted@"b1"}}, 
         {"c", {"c1", "c2", "c3", "c4"}}}
list2 = {{"a", {"aa1", "aa2", "aa3"}}, {"b", {"bb1", "bb2", "bb3", "bb4"}}, 
         {"c", {"cc1", "cc2", "cc3"}}}

KeyValueMap[{#1, Union @@ #2} &] @ GroupBy[Join[list1, list2], First -> Last]

Mathematica graphics

Hope this helps.

$\endgroup$
0
$\begingroup$

Using GatherBy[]

list1 = {{"a", {"a1", "a2", "a3", "a4"}}, {"b", {"b1"}}, 
         {"c", {"c1", "c2", "c3", "c4"}}}

list2 = {{"a", {"aa1", "aa2", "aa3"}}, {"b", {"bb1", "bb2", "bb3", "bb4"}},
         {"c", {"cc1", "cc2", "cc3"}}}


{#1[[1]], Join[#1[[2]], #2[[2]]]} & @@@ GatherBy[Join[list1, list2], First]

Or

Join[list1, list2, 2] /. {x_, y_, x_, z_} :> {x, Join[y, z]}
{{"a", {"a1", "a2", "a3", "a4", "aa1", "aa2", "aa3"}}, 
 {"b", {"b1", "bb1", "bb2", "bb3", "bb4"}}, 
 {"c", {"c1", "c2", "c3", "c4", "cc1","cc2", "cc3"}}}
$\endgroup$
2
  • 1
    $\begingroup$ , sir there is a comment in the first list that needs to be added back in the result. $\endgroup$
    – Phenom
    Commented Mar 19, 2016 at 7:17
  • $\begingroup$ Can anyone help please!? $\endgroup$
    – Phenom
    Commented Mar 19, 2016 at 7:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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