Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #3405

Re: How can I concatenate elements

From Stefan Salanski <wutchamacallit27@gmail.com>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: How can I concatenate elements
Date 2011-06-30 20:54 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <iuinps$kuc$1@smc.vnet.net> (permalink)
References <iu4a05$jpm$1@smc.vnet.net>

Show all headers | View raw


On Jun 25, 5:32 am, "Leandro Tenfen" <leandroten...@hotmail.com>
wrote:
> Hi,
>
> I have two lists:
>
> s1={1,2,3,4,5,6};
> s2={c,k};
>
> How can I concatenate elements of lists as follows:
>
> s3={1c,2c,3c,4c,5c,6c,1k,2k,3k,4k,5k,6k}
>
> Many Thanks!

It looks like Outer is the function which fits your problem best. Just
use StringJoin as your product function.
To use StringJoin, you will need to have both lists be lists of
strings, so {"1", "2", "3", ..etc... } not just {1,2,3...}, and
{"c","k"} not just {c,k}.


In[21]:= s1 = ToString /@ Range[6];
            s2 = {"c", "k"};

In[23]:= Flatten@Outer[StringJoin, s1, s2]

Out[23]= {"1c", "1k", "2c", "2k", "3c", "3k", "4c", "4k", "5c", "5k",
"6c", "6k"}

if you want the resulting list in the order you specified, you can
either sort that list.. though you'd have to sort it starting from the
end of the string, so like
In[24]:= StringReverse /@ Sort[StringReverse /@ %]

Out[24]= {"1c", "2c", "3c", "4c", "5c", "6c", "1k", "2k", "3k", "4k",
"5k", "6k"}

Or you can just switch the order of the lists in Outer, and the order
of the elements passed to StringJoin like this:

In[25]:= Flatten@Outer[StringJoin[#2, #1] &, s2, s1]

Out[25]= {"1c", "2c", "3c", "4c", "5c", "6c", "1k", "2k", "3k", "4k",
"5k", "6k"}

Hope that helps

-Stefan Salanski

Back to comp.soft-sys.math.mathematica | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

How can I concatenate elements "Leandro Tenfen" <leandrotenfen@hotmail.com> - 2011-06-25 09:32 +0000
  Re: How can I concatenate elements Oleksandr Rasputinov <oleksandr_rasputinov@hmamail.com> - 2011-06-26 10:32 +0000
  Re: How can I concatenate elements Oleksandr Rasputinov <oleksandr_rasputinov@hmamail.com> - 2011-06-26 21:41 +0000
  Re: How can I concatenate elements Armand Tamzarian <mike.honeychurch@gmail.com> - 2011-06-29 21:27 +0000
  Re: How can I concatenate elements sungmin <sungmin817@googlemail.com> - 2011-06-29 21:28 +0000
  Re: How can I concatenate elements Orleo Marinaro <orleo.marinaro@gmail.com> - 2011-06-29 21:52 +0000
  Re: How can I concatenate elements Orleo Marinaro <orleo.marinaro@gmail.com> - 2011-06-29 21:52 +0000
  Re: How can I concatenate elements Stefan Salanski <wutchamacallit27@gmail.com> - 2011-06-29 21:53 +0000
  Re: How can I concatenate elements Orleo Marinaro <orleo.marinaro@gmail.com> - 2011-06-30 20:53 +0000
  Re: How can I concatenate elements Stefan Salanski <wutchamacallit27@gmail.com> - 2011-06-30 20:54 +0000
  Re: How can I concatenate elements Orleo Marinaro <orleo.marinaro@gmail.com> - 2011-06-30 20:52 +0000

csiph-web