Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I've a list of lists representing k-rows of truth table having n elements each in a nested list consider

 lst={{True, True, True}, {True, True, False}, {True, False, True}, {True, 
    False, False}, {False, True, True}, {False, True, False}, {False, 
    False, True}, {False, False, False}}

i want to get all those sublists having n-1 elements same but differ by jth element

e.g. if i want the 2nd element to of a given list to be different i should get the following result group:

 lst={{{True, True, True},  {True, False, True}}, {{True, True, False}, {True, 
 False, False}}, {{False, True, True}, {False, 
 False, True}},{{False, True, False}, {False, False, False}}}

similarly, if i want the third element to be differnt i should get this:

lst={{{True, True, True}, {True, True, False}}, {{True, False, True}, {True, 
  False, False}}, {{False, True, True}, {False, True, False}}, {{False, 
  False, True}, {False, False, False}}}

I'll be thankful for anyone helping out.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

Given your example data:

Module[{l = #, ll = Length@#[[1]], ta, s},
   ta = ConstantArray[True, ll];
   ta[[#2]] = False;
   s = Subsets[l, {2}];
   s[[Flatten@Position[SameQ @@@ Transpose@# & /@ s, ta]]]] &[lst, 2]

(*
{{{True, True, True}, {True, False, True}}, {{True, True, 
   False}, {True, False, False}}, {{False, True, True}, {False, False,
    True}}, {{False, True, False}, {False, False, False}}}
*)

Probably much faster, certainly more concise:

With[{l = #, c = Complement[Range@Length@#[[1]], {#2}]},
     Cases[GatherBy[l, #[[c]] &], {_, __}]] &[lst, dp]

In either, first argument is your list, second argument is the element position in sublists you wish to differ.

share|improve this answer
    
Thankyou very much. –  aark May 20 at 20:24

Your Answer

 
discard

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 or ask your own question.