2
$\begingroup$

I have a list and I want to choose randomly n elements, but each time that draw a element I need to delete the other elements whit some in common:

list= {{2, 1}, {3, 1}, {3, 2}, {4, 1}, {4, 2}, {4, 3}, {5, 2}, {5, 3}, {5, 
 4}, {6, 1}, {6, 3}, {6, 4}, {6, 5}, {7, 1}, {7, 2}, {7, 4}, {7, 
 5}, {7, 6}, {8, 1}, {8, 2}, {8, 3}, {8, 5}, {8, 6}, {8, 7}, {9, 
 2}, {9, 3}, {9, 4}, {9, 6}, {9, 7}, {9, 8}, {10, 1}, {10, 3}, {10, 
 4}, {10, 5}, {10, 7}, {10, 8}, {10, 9}, {11, 1}, {11, 2}, {11, 
 4}, {11, 5}, {11, 6}, {11, 8}, {11, 9}, {11, 10}, {12, 1}, {12, 
 2}, {12, 3}, {12, 5}, {12, 6}, {12, 7}, {12, 9}, {12, 10}, {12, 
 11}, {1, 2}, {1, 3}, {2, 3}, {1, 4}, {2, 4}, {3, 4}, {2, 5}, {3, 
 5}, {4, 5}, {1, 6}, {3, 6}, {4, 6}, {5, 6}, {1, 7}, {2, 7}, {4, 
 7}, {5, 7}, {6, 7}, {1, 8}, {2, 8}, {3, 8}, {5, 8}, {6, 8}, {7, 
 8}, {2, 9}, {3, 9}, {4, 9}, {6, 9}, {7, 9}, {8, 9}, {1, 10}, {3, 
 10}, {4, 10}, {5, 10}, {7, 10}, {8, 10}, {9, 10}, {1, 11}, {2, 
 11}, {4, 11}, {5, 11}, {6, 11}, {8, 11}, {9, 11}, {10, 11}, {1, 
 12}, {2, 12}, {3, 12}, {5, 12}, {6, 12}, {7, 12}, {9, 12}, {10, 
 12}, {11, 12}, {5, 1}, {6, 2}, {7, 3}, {8, 4}, {9, 1}, {9, 5}, {10, 
 2}, {10, 6}, {11, 3}, {11, 7}, {12, 4}, {12, 8}, {1, 5}, {2, 6}, {3,
 7}, {4, 8}, {1, 9}, {5, 9}, {2, 10}, {6, 10}, {3, 11}, {7, 11}, {4,
 12}, {8, 12}}

Then if I draw {2,12} I need to delete all elements that have 2 or/and 12 {{12,2},{2, 1},{3,2},{4,2}....} I need to have into account the weights:

list2={0.27, 0.27, 0.27, 0.27, 0.27, 0.18, 0.27, 0.18, 0.18, 0.27, 0.18, \
 0.18, 0.27, 0.27, 0.27, 0.18, 0.27, 0.27, 0.27, 0.27, 0.18, 0.27, \
 0.27, 0.18, 0.27, 0.18, 0.18, 0.27, 0.18, 0.18, 0.27, 0.18, 0.18, \
 0.27, 0.18, 0.18, 0.27, 0.27, 0.27, 0.18, 0.27, 0.27, 0.18, 0.27, \
 0.27, 0.27, 0.27, 0.18, 0.27, 0.27, 0.18, 0.27, 0.27, 0.18, 0.27, \
 0.18, 0.18, 0.18, 0.18, 0.18, 0.27, 0.27, 0.27, 0.27, 0.27, 0.27, \
 0.27, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, \
 0.18, 0.27, 0.27, 0.27, 0.27, 0.27, 0.27, 0.27, 0.27, 0.27, 0.27, \
 0.27, 0.27, 0.27, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, \
 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.37, 0.37, \
 0.28, 0.28, 0.37, 0.37, 0.37, 0.37, 0.28, 0.28, 0.28, 0.28, 0.37, \
 0.37, 0.28, 0.28, 0.37, 0.37, 0.37, 0.37, 0.28, 0.28, 0.28, 0.28}
$\endgroup$
3
$\begingroup$

Maybe this is what you look for:

step[{pairs_, weights_}] := Module[{choice, pos},
  choice = RandomChoice[weights -> pairs];
  pos = Position[pairs, 
    Alternatives @@ 
     Map[x \[Function] Sequence @@ {{x, _}, {_, x}}, choice], 1];
  {
   choice,
   {Delete[pairs, pos], Delete[weights, pos]}
   }
  ]

You may use it as follows:

{choice, {newlist, newlist2}} = step[{list, list2}]

Of course, you can use that repeatedly on order to choose n elements.

$\endgroup$
  • $\begingroup$ @H Schumacher . Congratulations it works nice and clean, Now need to study how you use Alternatives, [Function] $\endgroup$ – Anxon Pués Apr 4 '18 at 19:55
  • $\begingroup$ \[Function] is the infix form of Function. Alternatives is for creating a pattern that matches your needs, so that Position can do the lookup. $\endgroup$ – Henrik Schumacher Apr 4 '18 at 19:58
  • $\begingroup$ Nice! @Henrik Schumacher. ...but...isn´t clear for me the Sequence. I went to Help but i don´t understand exactly the: Sequence @@ {{x, _}, {_, x}}.. $\endgroup$ – Ask8 Apr 5 '18 at 2:32
  • $\begingroup$ @@ is the infix form of Apply and Sequence is like List "but without braces". So, Sequence @@ {{x, _}, {_, x}} is a programmatic way to turn {{x, _}, {_, x}} into {x, _}, {_, x}. Note that Map[x \[Function] {{x, _}, {_, x}}, {a,b}] would return {{{a,_},{_,a}}, {{b,_},{_,b}}} but we want {{a,_},{_,a}, {b,_},{_,b}} here. $\endgroup$ – Henrik Schumacher Apr 5 '18 at 6:10

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.