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


Groups > comp.soft-sys.math.mathematica > #3300 > unrolled thread

How can I concatenate elements

Started by"Leandro Tenfen" <leandrotenfen@hotmail.com>
First post2011-06-25 09:32 +0000
Last post2011-06-30 20:52 +0000
Articles 11 — 6 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  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

#3300 — How can I concatenate elements

From"Leandro Tenfen" <leandrotenfen@hotmail.com>
Date2011-06-25 09:32 +0000
SubjectHow can I concatenate elements
Message-ID<iu4a05$jpm$1@smc.vnet.net>
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!

[toc] | [next] | [standalone]


#3316

FromOleksandr Rasputinov <oleksandr_rasputinov@hmamail.com>
Date2011-06-26 10:32 +0000
Message-ID<iu71r3$1cg$1@smc.vnet.net>
In reply to#3300
In[1] := s1 = {1, 2, 3, 4, 5, 6}; s2 = {c, k};

In[3] := s3 = Flatten@Outer[Times, s2, s1]

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

On Jun 25, 10: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!

[toc] | [prev] | [next] | [standalone]


#3333

FromOleksandr Rasputinov <oleksandr_rasputinov@hmamail.com>
Date2011-06-26 21:41 +0000
Message-ID<iu891c$7d9$1@smc.vnet.net>
In reply to#3300
In[1] := s1 = {1, 2, 3, 4, 5, 6}; s2 = {c, k};

In[3] := s3 = Flatten@Outer[Times, s2, s1]

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

On Jun 25, 10: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!

[toc] | [prev] | [next] | [standalone]


#3345

FromArmand Tamzarian <mike.honeychurch@gmail.com>
Date2011-06-29 21:27 +0000
Message-ID<iug5be$9dr$1@smc.vnet.net>
In reply to#3300
On Jun 25, 7:32 pm, "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 depends on the form of your elements. For example:

Distribute[f[s1, s2], List] /. f -> Times

for if you have or want strings then:

g[x_, y_] := ToString[x] <> ToString[y]

Distribute[f[s1, s2], List] /. f -> g

Maybe a more detailed example/description would help.


Mike

[toc] | [prev] | [next] | [standalone]


#3346

Fromsungmin <sungmin817@googlemail.com>
Date2011-06-29 21:28 +0000
Message-ID<iug5eb$9e7$1@smc.vnet.net>
In reply to#3300
How about this?

s1 # & /@ s2 // Flatten

Sungmin

On Jun 25, 6:32 pm, "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!

[toc] | [prev] | [next] | [standalone]


#3398

FromOrleo Marinaro <orleo.marinaro@gmail.com>
Date2011-06-29 21:52 +0000
Message-ID<iug6q8$9mp$1@smc.vnet.net>
In reply to#3300
Use Outer[]
So
Outer[Times, s2, s1]
and the out is
{{c, 2 c, 3 c, 4 c, 5 c, 6 c}, {k, 2 k, 3 k, 4 k, 5 k, 6 k}}
Then
% // Flatten
and yield
{c, 2 c, 3 c, 4 c, 5 c, 6 c, k, 2 k, 3 k, 4 k, 5 k, 6 k}


On Jun 25, 11: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!

[toc] | [prev] | [next] | [standalone]


#3400

FromOrleo Marinaro <orleo.marinaro@gmail.com>
Date2011-06-29 21:52 +0000
Message-ID<iug6r7$9mr$1@smc.vnet.net>
In reply to#3300
Use the built-in Join[].
So
Join[s1,s2]
and yield
{1, 2, 3, 4, 5, 6, c, k}


On Jun 25, 11: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!

[toc] | [prev] | [next] | [standalone]


#3401

FromStefan Salanski <wutchamacallit27@gmail.com>
Date2011-06-29 21:53 +0000
Message-ID<iug6t4$9n9$1@smc.vnet.net>
In reply to#3300
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

[toc] | [prev] | [next] | [standalone]


#3403

FromOrleo Marinaro <orleo.marinaro@gmail.com>
Date2011-06-30 20:53 +0000
Message-ID<iuinnv$ku6$1@smc.vnet.net>
In reply to#3300
Use the built-in Join[].
So
Join[s1,s2]
and yield
{1, 2, 3, 4, 5, 6, c, k}


On Jun 25, 11: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!

[toc] | [prev] | [next] | [standalone]


#3405

FromStefan Salanski <wutchamacallit27@gmail.com>
Date2011-06-30 20:54 +0000
Message-ID<iuinps$kuc$1@smc.vnet.net>
In reply to#3300
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

[toc] | [prev] | [next] | [standalone]


#3413

FromOrleo Marinaro <orleo.marinaro@gmail.com>
Date2011-06-30 20:52 +0000
Message-ID<iuinn0$ku0$1@smc.vnet.net>
In reply to#3300
Use Outer[]
So
Outer[Times, s2, s1]
and the out is
{{c, 2 c, 3 c, 4 c, 5 c, 6 c}, {k, 2 k, 3 k, 4 k, 5 k, 6 k}}
Then
% // Flatten
and yield
{c, 2 c, 3 c, 4 c, 5 c, 6 c, k, 2 k, 3 k, 4 k, 5 k, 6 k}


On Jun 25, 11: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!

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web