how to concatenate a list of a list of strings
Here's an approach for any two multi-dimensional lists of strings which have arbitrary, but matching structures:
stringJoin[x__String] := StringJoin[x]
SetAttributes[stringJoin, Listable]
stringJoin[La, Lb]
EDIT
Short explanation of listability:
Listable functions are effectively applied separately to each element in a list, or to corresponding elements in each list if there is more than one list.
In order to prevent premature evaluation of stringJoin[a,b]
where a
and b
are lists, e.g. a = {a1,a2,a3}; b = {b1,b2,b3}
leading to "a1a2a3b1b2b3"
I have stringJoin
accept only String
arguments. Then it keeps drilling down to the lowest level until it finally does find a string.
Here's another example. Setting a function f
to be listable is almost like replacing every occurrence of f[...]
with Thread[f[...]]
.
list = {a, b, {c, d}, {e, f, g, h}, {i, j, k}}
f[list, list]
(* f[{a, b, {c, d}, {e, f, g, h}, {i, j, k}},
{a, b, {c, d}, {e, f, g, h}, {i, j, k}}] *)
Now:
Thread[f[list, list]]
{f[a, a], f[b, b],
f[{c, d}, {c, d}], f[{e, f, g, h}, {e, f, g, h}], f[{i, j, k}, {i, j, k}]}
ReplaceRepeated
can allow emulation of listability:
f[list, list] //. f[x_, y_] :> Thread[f[x, y]]
(* {f[a, a], f[b, b], {f[c, c], f[d, d]}, {f[e, e], f[f, f], f[g, g], f[h, h]}, {f[i, i], f[j, j], f[k, k]}} *)
Or we can simply do SetAttributes[f, Listable]; f[list, list]
.
sLa = Map[ToString, La, {2}];
sLb = Map[ToString, Lb, {2}];
MapThread[StringJoin, #] & /@ Transpose[{sLa, sLb}]
also
Thread[j @@ #] & /@ Transpose[{sLa, sLb}] /. j -> StringJoin