7
$\begingroup$

From the list

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

how can I select exactly those 3-tuples that contain one for more nines?

This is the result I want:

{{1, 1, 9}, {1, 2, 9}, {1, 3, 9}, {1, 4, 9}, {1, 9, 1}  ... {9, 9, 4}, {9, 9, 9}}
$\endgroup$
  • 2
    $\begingroup$ Help > Wolfram Docuentation, enter "select" (with or without quotes). First hit is to Select... $\endgroup$ – Daniel Lichtblau Jun 2 at 11:59
8
$\begingroup$
list=Tuples[{1, 2, 3, 4, 9}, 3] ;
Cases[{___,9,___}]@list

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

$\endgroup$
6
$\begingroup$

try

Select[Tuples[{1,2,3,4,9},3],MemberQ@9]   

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

$\endgroup$
  • $\begingroup$ This can even be shortened to Select[Tuples[{1,2,3,4,9},3], MemberQ[9]]. $\endgroup$ – Henrik Schumacher Jun 2 at 9:36
  • $\begingroup$ @HenrikSchumacher nice! $\endgroup$ – J42161217 Jun 2 at 9:58
2
$\begingroup$
L = Tuples[{1, 2, 3, 4, 9}, 3];

Pick[L, Total /@ UnitBox[L - 9], 1 | 2 | 3]

Or

Pick[L, Total /@ UnitStep[L - 8], 1 | 2 | 3]
$\endgroup$
1
$\begingroup$
lst // Pick[#, Times@@@(# /. 9-> 0), 0] &

and

Pick[lst,Times@@@(lst/. 9-> 0),0]==
Pick[lst, MemberQ[#,9]&/@lst]
$\endgroup$

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