4
$\begingroup$

I have a list:

{{Subscript[i,  1], Subscript[a,  1], Subscript[b,  1]},
 {Subscript[i,  1], Subscript[a,  2], Subscript[b,  2]},
 ...,
 {Subscript[i,  r], Subscript[b,  1], Subscript[a,  1]},
 ...,
 {Subscript[i,  s], Subscript[b,  2], Subscript[a,  2]},
 ...,
 {Subscript[i,  n], Subscript[a,  n], Subscript[b,  n]}
}

in other words; this is a list indexed by:

{Subscript[i, 1], Subscript[i, 2], ..., Subscript[i, n]}

and every now and then; there are instances in the list where: {Subscript[a, i], Subscript[b, i]} is considered the same as: {Subscript[b, i], Subscript[a, i]}.

For example; if two jobs contained in the above list; namely,

{"Bill and Account Collectors", "Ticket Agents and Travel Clerks"}

have a job relational score of 0.5; then

{"Ticket Agents and Travel Clerks", "Bill and Account Collectors"}

also have the same relational score.

My question is; how can I collapse the above list to get a new list so that; if

{Subscript[a, i], Subscript[b, i]} == {Subscript[b, i], Subscript[a, i]}

then only

{Subscript[i, s], {Subscript[a, i], Subscript[b, i]}}

is included in my new list; and if:

{Subscript[a, k], Subscript[b, k]} not equal {Subscript[b, k], Subscript[a, k]}

then both:

{Subscript[i, s], {Subscript[a, k], Subscript[b, k]}}

and

{Subscript[i, t], {Subscript[a, k], Subscript[b, k]}}

are also included in my new list?

As an example, if

IndexedSOCList = {{1., 431011., 431011.}, {2., 431011., 433011.}, {3., 433011., 431011.},
   {4., 431011., 433021.}, {5., 433021., 431011.}, {6., 431011., 433031.},
   {7., 433031., 431011.}, {8., 431011., 433051.}, {9., 433051., 431011.},
   {10., 431011., 433061.}, {11., 433061., 431011.}, {12., 431011., 433071.},
   {13., 433071., 431011.}, {14., 431011., 434011.}, {15., 434011., 431011.},
   {16., 431011., 434031.}, {17., 434031., 431011.}, {18., 431011., 434041.},
   {19., 434041., 431011.}};

then the desired output is

(* {{1., 431011., 431011.}, {2., 431011., 433011.}, {4., 431011., 433021.},
    {6., 431011., 433031.}, {8., 431011., 433051.}, {10., 431011., 433061.},
    {12., 431011., 433071.}, {14., 431011., 434011.}, {16., 431011., 434031.},
    {18., 431011., 434041.}} *)
$\endgroup$
  • $\begingroup$ One possibility, I think? Union[list, SameTest -> (Sort[#1[[2]]] === Sort[#2[[2]]] &)]. $\endgroup$ – march Oct 3 '16 at 21:15
  • $\begingroup$ IndexedSOCList={{1., 431011., 431011.}, {2., 431011., 433011.}, {3., 433011., 431011.}, {4., 431011., 433021.}, {5., 433021., 431011.}, {6., 431011., 433031.}, {7., 433031., 431011.}, {8., 431011., 433051.}, {9., 433051., 431011.}, {10., 431011., 433061.}, {11., 433061., 431011.}, {12., 431011., 433071.}, {13., 433071., 431011.}, {14., 431011., 434011.}, {15., 434011., 431011.}, {16., 431011., 434031.}, {17., 434031., 431011.}, {18., 431011., 434041.}, {19., 434041., 431011.}}; $\endgroup$ – Gilmar Rodriguez Pierluissi Oct 3 '16 at 21:38
  • $\begingroup$ FilteredIndexedSOCList = Union[IndexedSOCList, SameTest -> (Sort[#1[[2]]] === Sort[#2[[2]]] &)] $\endgroup$ – Gilmar Rodriguez Pierluissi Oct 3 '16 at 21:39
  • $\begingroup$ "Nonatomic expression expected at position 1 in Sort[431011.`" "Further output will be suppressed during this calculation" $\endgroup$ – Gilmar Rodriguez Pierluissi Oct 3 '16 at 21:40
  • $\begingroup$ The list in your comment is different in structure than the list in your post. To match your post, it would need to be IndexedSOCList={{1., {431011., 431011.}}, {2., {431011., 433011.}}, {3., {433011., 431011.}}, ... , in which case my code works (if I understood what you were after, of course). If the format in your comment is the actual correct one, then try Union[IndexedSOCList, SameTest -> (Sort[#1[[2 ;; 3]]] === Sort[#2[[2 ;; 3]]] &)]. $\endgroup$ – march Oct 3 '16 at 21:41
4
$\begingroup$

We use Union with a custom SameTest:

Union[IndexedSOCList, SameTest -> (Sort[#1[[2 ;; 3]]] === Sort[#2[[2 ;; 3]]] &)]

Alternatively, use DeleteDuplicatesBy:

DeleteDuplicatesBy[IndexedSOCList, Sort[#[[2 ;; 3]]] &]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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