5
$\begingroup$

In List:

Tuples[{1, 2, 3, 4, 9}, 4]

How can I select all items that contain only two "9"?

Result:

{{1, 1, 9, 9}, {1, 2, 9, 9}, {1, 3, 9, 9}, {1, 4, 9, 9}, {1, 9, 1, 9} ... {9, 9, 4, 3}, {9, 9, 4, 4}}

$\endgroup$

closed as off-topic by AccidentalFourierTransform, LCarvalho, MarcoB, Alex Trounev, Carl Lange Jun 5 at 0:28

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – AccidentalFourierTransform, LCarvalho, MarcoB, Alex Trounev, Carl Lange
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 5
    $\begingroup$ Try Select[alist, Count[#, 9] == 2 &]. $\endgroup$ – b.gates.you.know.what Jun 2 at 8:22
  • $\begingroup$ Thanks! b.gates.you.know.what $\endgroup$ – Carbajo Jun 2 at 8:25
2
$\begingroup$
list=Tuples[{1, 2, 3, 4, 9}, 4];
Cases[_?(Count[#, 9] == 2&)] @ list
Pick[list, Lookup[Counts /@ list, 9, 0] - 2, 0]

both give

{{1, 1, 9, 9}, {1, 2, 9, 9}, {1, 3, 9, 9}, {1, 4, 9, 9}, {1, 9, 1, 9}, {1, 9, 2, 9}, {1, 9, 3, 9}, {1, 9, 4, 9}, {1, 9, 9, 1}, {1, 9, 9, 2}, {1, 9, 9, 3}, {1, 9, 9, 4}, {2, 1, 9, 9}, {2, 2, 9, 9}, {2, 3, 9, 9}, {2, 4, 9, 9}, {2, 9, 1, 9}, {2, 9, 2, 9}, {2, 9, 3, 9}, {2, 9, 4, 9}, {2, 9, 9, 1}, {2, 9, 9, 2}, {2, 9, 9, 3}, {2, 9, 9, 4}, {3, 1, 9, 9}, {3, 2, 9, 9}, {3, 3, 9, 9}, {3, 4, 9, 9}, {3, 9, 1, 9}, {3, 9, 2, 9}, {3, 9, 3, 9}, {3, 9, 4, 9}, {3, 9, 9, 1}, {3, 9, 9, 2}, {3, 9, 9, 3}, {3, 9, 9, 4}, {4, 1, 9, 9}, {4, 2, 9, 9}, {4, 3, 9, 9}, {4, 4, 9, 9}, {4, 9, 1, 9}, {4, 9, 2, 9}, {4, 9, 3, 9}, {4, 9, 4, 9}, {4, 9, 9, 1}, {4, 9, 9, 2}, {4, 9, 9, 3}, {4, 9, 9, 4}, {9, 1, 1, 9}, {9, 1, 2, 9}, {9, 1, 3, 9}, {9, 1, 4, 9}, {9, 1, 9, 1}, {9, 1, 9, 2}, {9, 1, 9, 3}, {9, 1, 9, 4}, {9, 2, 1, 9}, {9, 2, 2, 9}, {9, 2, 3, 9}, {9, 2, 4, 9}, {9, 2, 9, 1}, {9, 2, 9, 2}, {9, 2, 9, 3}, {9, 2, 9, 4}, {9, 3, 1, 9}, {9, 3, 2, 9}, {9, 3, 3, 9}, {9, 3, 4, 9}, {9, 3, 9, 1}, {9, 3, 9, 2}, {9, 3, 9, 3}, {9, 3, 9, 4}, {9, 4, 1, 9}, {9, 4, 2, 9}, {9, 4, 3, 9}, {9, 4, 4, 9}, {9, 4, 9, 1}, {9, 4, 9, 2}, {9, 4, 9, 3}, {9, 4, 9, 4}, {9, 9, 1, 1}, {9, 9, 1, 2}, {9, 9, 1, 3}, {9, 9, 1, 4}, {9, 9, 2, 1}, {9, 9, 2, 2}, {9, 9, 2, 3}, {9, 9, 2, 4}, {9, 9, 3, 1}, {9, 9, 3, 2}, {9, 9, 3, 3}, {9, 9, 3, 4}, {9, 9, 4, 1}, {9, 9, 4, 2}, {9, 9, 4, 3}, {9, 9, 4, 4}}

$\endgroup$
  • $\begingroup$ @Carbajo, thank you for the accept. But I would go for Coolwater's method; it is several orders faster. $\endgroup$ – kglr Jun 2 at 9:24
7
$\begingroup$

E.g.

L = Tuples[{1, 2, 3, 4, 9}, 4];

Pick[L, Total[Clip[L, {9, 9}, {0, 0}], {2}], 18]

If you need it repeatably, you could make a list of parts

parts = Position[L, _?(Count[#, 9] == 2 &), {1}][[All, 1]];

and simply write L[[parts]]

$\endgroup$
3
$\begingroup$
L = Tuples[{1, 2, 3, 4, 9}, 4];

Pick[L, Total /@ UnitStep[L - 8], 2]

Or

Pick[L, Total /@ UnitBox[L - 9], 2]
$\endgroup$

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