The Math Forum

Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Math Forum » Discussions » Inactive » comp.soft-sys.math.mathematica

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Distribute one list element over another
Replies: 4   Last Post: Apr 15, 2013 2:28 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Tomas Garza Hernandez

Posts: 264
Registered: 4/15/05
Re: Distribute one list element over another
Posted: Apr 15, 2013 2:28 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

In[1]:= data={{a1,a2,a3,{m1,m2,m3,m4}},{b1,b2,b3,{n1,n2,n3,n4}},{c1,c2,c3,{o1,o2,o3,o4}},{d1,d2,d3,{p1,p2,p3,p4}}}Out[1]= {{a1,a2,a3,{m1,m2,m3,m4}},{b1,b2,b3,{n1,n2,n3,n4}},{c1,c2,c3,{o1,o2,o3,o4}},{d1,d2,d3,{p1,p2,p3,p4}}}
In[2]:= Table[{data[[j,1]],#}&/@Last[data[[j]]],{j,1,Length[data]}]Out[2]= {{{a1,m1},{a1,m2},{a1,m3},{a1,m4}},{{b1,n1},{b1,n2},{b1,n3},{b1,n4}},{{c1,o1},{c1,o2},{c1,o3},{c1,o4}},{{d1,p1},{d1,p2},{d1,p3},{d1,p4}}}

> From:
> Subject: Distribute one list element over another
> To:
> Date: Sat, 13 Apr 2013 20:38:35 -0400
> Hallo,
> It's difficult to explain what I want to achieve and maybe if I could
> describe it better I would have been able to google a solution.
> My problem deals with lists of lists of the form {a1, a2, a3, {m1, m2,
> m3, m4}}
> e.g.
> data =
> {
> {a1, a2, a3, {m1, m2, m3, m4}},
> {b1, b2, b3, {n1, n2, n3, n4}},
> {c1, c2, c3, {o1, o2, o3, o4}},
> {d1, d2, d3, {p1, p2, p3, p4}},
> }
> I want is to go over the list and transform each of the sublists {a1,
> a2, a3, {m1, m2, m3, m4}} into a list consisting of pairs {{a1, m1},
> {a1, m2}, {a1, m3}, {a1, m4}}
> so that for the list given above I get
> {
> {{a1, m1}, {a1, m2}, {a1, m3}, {a1, m4}},
> {{b1, n1}, {b1, n2}, {b1, n3}, {b1, n4}},
> {{c1, o1}, {c1, o2}, {c1, o3}, {c1, o4}},
> {{d1, p1}, {d1, p2}, {d1, p3}, {d1, p4}},
> }
> I think it could be possible to do this using two nested map operations
> /@, but I don't find out how to do so exactly, because I run into
> trouble with the scope of the #
> What I'm doing at the moment is that I use a module:
> verteilen[liste_] := Module[{},
> {#[[1]], liste[[1]]} & /@ liste[[4]]
> ]
> and map it over the list I want to transform:
> verteilen[#] & /@ data
> I'm searching for a much mor elegant way to achive this transformation,
> any hints?
> Thanks and best regards
> Timm

Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2018. All Rights Reserved.