1
$\begingroup$

How can I generate a new tuples by joining each element of tup3 to tup1?
For example joining {0,1,1,1,0,-1} from tup1 and {d,0,1} from tup3 to generate {0,1,1,1,0,-1,d,0,1}

tup1 = Tuples@{{0, 1}, {-1, 0, 1}, {-1, 0, 1}, {0, 1}, {-1, 0, 
     1}, {-1, 0, 1}};
tup2 = Tuples[{{0, 1, -1}, {0, 1, -1}}];
tup3 = tup2 /. {a_, b_} :> {d, a, b}
$\endgroup$
3
  • $\begingroup$ Join @@@ Tuples[{tup1, tup3}]? $\endgroup$ – kglr Apr 4 at 19:37
  • $\begingroup$ @kglr well, that works. Do you have any method to get that directly from tup1 and tup2? Above I have to add one more step to add tup3 to add the "d". $\endgroup$ – anhnha Apr 4 at 19:39
  • $\begingroup$ try Join @@@ Tuples[{tup1, {{d}}, tup2}] $\endgroup$ – kglr Apr 4 at 19:48
2
$\begingroup$
tup4a = Join @@@ Tuples[{tup1, tup3}];
tup4a // Short
 {{0,-1,-1,0,-1,-1,d,0,0},{0,-1,-1,0,-1,-1,d,0,1},<<2912>>,
  {1,1,1,1,1,1,d,-1,1},{1,1,1,1,1,1,d,-1,-1}}
tup4b = Distribute[{tup1, tup3}, List, List, List, Join];

tup4a == tup4b
True

"to get that directly from tup1 and tup2":

tup4c = Join @@@ Tuples[{tup1, {{d}}, tup2}];

tup4a == tup4c
True
$\endgroup$
1
  • $\begingroup$ well, that looks simple now just apply Join at level 1 but I couldn't think of. $\endgroup$ – anhnha Apr 4 at 19:53

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