1
$\begingroup$

I want to split a list. There is one set of list with two {x,{a,b}} elements. it's like

list={① {{x1,{a1,b1}},{x2,{a2,b2}}},②{{x1,{a1,b1}},{x2,{a2,b2}}},③{{x1,{a1,b1}},{x2,{a2,b2}}}} (*and so on...*)

What function should I use to split the list when a1 and a2 are the same (a1=a2) and when they are different (a1≠a2)?

I created sample list below.

ListA={{{10, {1, 2}}, {10, {1, 2}}},{{2.5, {1, 2}, {17, {1, 1}}}, {{14, {2, 5}, {10, {1, 1}}},{{12.1, {1, 1},{3,{4,3}}}}

I want to split listA into lista and listb like this.

 lista(*a1=a2*)={{{10, {1, 2}}, {10, {1, 2}}},
        {{2.5, {1, 2}}, {17, {1, 1}}}}



listb(*a1≠a2*)={{{14, {2, 5}, {10, {1, 1}}},
       {{12.1, {1, 1}},{3,{4,3}}}}
$\endgroup$
3
  • 5
    $\begingroup$ The definition of ListA has unbalanced opening braces. Please use lower case first letters for user-defined symbols. $\endgroup$
    – Syed
    May 15, 2023 at 11:30
  • $\begingroup$ Thank you for your comment. I edited. $\endgroup$
    – hare
    May 15, 2023 at 15:28
  • 1
    $\begingroup$ hare, your edit does not address Syed's comment -- ListA contains unmatched braces. $\endgroup$
    – kglr
    May 15, 2023 at 17:05

1 Answer 1

1
$\begingroup$
ListA = 
  {{{10, {1, 2}}, {10, {1, 2}}}, 
   {{2.5, {1, 2}}, {17, {1, 1}}}, 
   {{14, {2, 5}}, {10, {1, 1}}}, 
   {{12.1, {1, 1}}, {3, {4, 3}}}};
GatherBy[ListA, Equal @@ Part[#, 1 ;; 2, 2, 1] &]

If you want the data to further show which case is which, maybe you want GroupBy rather than GatherBy:

GroupBy[ListA, Equal @@ Part[#, 1 ;; 2, 2, 1] &]

If you just want lista, then do this:

Select[ListA, Equal @@ Part[#, 1 ;; 2, 2, 1] &]

If you just want listb, then do this:

Select[ListA, Not[Equal @@ Part[#, 1 ;; 2, 2, 1]] &]
$\endgroup$
5
  • $\begingroup$ Thank you for your comment. I carried out "GatherBy[ListA, Equal @@ Part[#, 1 ;; 2, 2, 1] &]". Then, {{14, {2, 5}}, {10, {1, 1}}}and{{12.1, {1, 1}}, {3, {4, 3}} were also displayed.This means that (a1=2,a2=1)and(a1=1,a2=4), so it's a1≠a2. $\endgroup$
    – hare
    May 15, 2023 at 16:13
  • 1
    $\begingroup$ If possible please tell me what you mean by [#, 1 ;; 2, 2, 1] &] $\endgroup$
    – hare
    May 15, 2023 at 16:14
  • $\begingroup$ I thought you wanted both lista and listb. The result my solution provides is {lista,listb}. $\endgroup$
    – lericr
    May 15, 2023 at 17:01
  • $\begingroup$ In my solutions, I've used GatherBy, GroupBy and Select. Each of these take a function as the second argument that performs the filtering. Equal @@ Part[#, 1 ;; 2, 2, 1] & is a function. I suggest you look at the documentation for Part and Apply and Function. $\endgroup$
    – lericr
    May 15, 2023 at 17:15
  • $\begingroup$ I see. Thank you for your kind explanation! It will be helpful $\endgroup$
    – hare
    May 16, 2023 at 2:57

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.