Suppose I have the string lists {'a','b','c'}
and {'1','2','3'}
. How do I get Mathematica to generate the list {'a1','a2','a3','b1','b2','b3','c1','c2','c3'}
?
-
9$\begingroup$ Note that none of the lists in your question contain strings. $\endgroup$– Bob HanlonApr 11, 2022 at 12:36
5 Answers
You can also use Tuples
:
a = {"a", "b", "c"};
n = {"1", "2", "3"};
StringJoin @@@ Tuples[{a, n}]
m = {"a", "b", "c"};
n = {"1", "2", "3"};
Catenate[Outer[StringJoin, m, n]]
{"a1", "a2", "a3", "b1", "b2", "b3", "c1", "c2", "c3"}
alist = {"a", "b", "c"};
nlist = {"1", "2", "3"};
Flatten@Outer[StringJoin, alist, nlist]
{"a1", "a2", "a3", "b1", "b2", "b3", "c1", "c2", "c3"}
For further study:
Join @@ (StringJoin @@@ Thread[Join[{#}, {nlist}]] & /@ alist)
Distribute[{alist,nlist},List,List,List,StringJoin]
(* {a1, a2, a3, b1, b2, b3, c1, c2, c3} *)
where
alist = {"a", "b", "c"};
nlist = {"1", "2", "3"};
alist = {"a", "b", "c"};
nlist = {"1", "2", "3"};
Flatten@Table[alist[[i]] <> nlist[[j]], {j, 1, Length@nlist}, {i, 1, Length@alist}]
{"a1", "a2", "a3", "b1", "b2", "b3", "c1", "c2", "c3"}