I can use the following way to do some operation (in this case ToString
) for the first component of each element in a list:
{ToString@#[[1]], Rest@#}~Flatten~1 & /@ {{1, 2}, {3, 4, 5}}
However, I have a few questions:
{ToString@#[[1]], Rest@#}~Flatten~1 & /@ {{1,
2}, 2, {3, 4, 5}}
for obvious reason. How to make it also work in
this case? The expected output would be {{"1", 2}, 2, {"3", 4, 5}}
.{{0}, {1, "2"}, {3, "4", 5}, {6, "7", 9, 10}}
/.{#[[1]]->ToString[#[[1]]]}
) for this kind of operation? So please list all solutions you may think of regardless of the efficiency.Thanks a lot!
I didn't realize this was the same as Leonid's core function until I had written it. Still perhaps that says that this may be a little more transparent than his rather elaborate function.
lst = {{1, 2}, 2, {3, 4, 5}};
Replace[lst, {a_, b__} :> {ToString@a, b}, 1]
{{"1", 2}, 2, {"3", 4, 5}}
One can then use {x:Repeated[_, {4}], a_, b__} :> {x, ToString@a, b}, 1]
for the fifth index, etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With