From: divisor on 19 Mar 2010 03:47 Hello mathGroup: I have an expression like this: b[ a[c], a[c], a[d]] a list like this: {e,f,g} I want to end up with b[ a[e], a[f], a[g]] I think of this as interleaving a list into an expression, but all my tries with ./,.//,MapIndexed[],MapAt[], Partition[Riffle[]] have come to no avail. Any help on this is greatly appreciated. Roger Williams Franklin Laboratory http://www.youtube.com/congruentlight
From: Ray Koopman on 19 Mar 2010 07:44 On Mar 19, 12:47 am, divisor <congruentialumina...(a)yahoo.com> wrote: > Hello mathGroup: > > I have an expression like this: > > b[ a[c], a[c], a[d]] > > a list like this: > > {e,f,g} > > I want to end up with > > b[ a[e], a[f], a[g]] > > I think of this as interleaving a list into an expression, but all my > tries with ./,.//,MapIndexed[],MapAt[], Partition[Riffle[]] have come > to no avail. > > Any help on this is greatly appreciated. > > Roger Williams > Franklin Laboratoryhttp://www.youtube.com/congruentlight In[1]:= b @@ a /@ {e,f,g} Out[1]= b[a[e],a[f],a[g]]
From: Albert Retey on 19 Mar 2010 07:44 Hi, > > I have an expression like this: > > b[ a[c], a[c], a[d]] > > a list like this: > > {e,f,g} > > I want to end up with > > b[ a[e], a[f], a[g]] > > I think of this as interleaving a list into an expression, but all my > tries with ./,.//,MapIndexed[],MapAt[], Partition[Riffle[]] have come > to no avail. > > Any help on this is greatly appreciated. > > Roger Williams > Franklin Laboratory > http://www.youtube.com/congruentlight one way with MapIndexed: expr = b[a[c], a[c], a[d]] list = {e, f, g} MapIndexed[Head[#1][list[[#2[[1]]]]] &, expr] hth, albert
From: Leonid Shifrin on 19 Mar 2010 07:44 Hi Roger, I am not sure if I understood correclty what you want, but you can use the following In[2]:= Clear[replaceLeaves]; replaceLeaves[expr_, lst_List] /; Length[Level[expr, {-1}]] == Length[lst] := Module[{n = 1}, MapIndexed[lst[[n++]] &, expr, {-1}]]; to replace all leaves in your expression, in the order corresponding to a depth-first traversal of an expression, by consecutive elements of another list. For instance, In[4]:= replaceLeaves[b[a[c], a[c], a[d]], {e, f, g}] Out[4]= b[a[e], a[f], a[g]] Here is another alternative: Clear[replaceLeavesAlt]; replaceLeavesAlt[expr_, lst_List] /; Length[Level[expr, {-1}]] == Length[lst] := ReplacePart[expr, lst, Position[expr, _, {-1}, Heads -> False], List /@ Range[Length[lst]]] In[11]:= replaceLeavesAlt[b[a[c], a[c], a[d]], {e, f, g}] Out[11]= b[a[e], a[f], a[g]] Regards, Leonid On Fri, Mar 19, 2010 at 10:46 AM, divisor <congruentialuminaire(a)yahoo.com>wrote: > Hello mathGroup: > > I have an expression like this: > > b[ a[c], a[c], a[d]] > > a list like this: > > {e,f,g} > > I want to end up with > > b[ a[e], a[f], a[g]] > > I think of this as interleaving a list into an expression, but all my > tries with ./,.//,MapIndexed[],MapAt[], Partition[Riffle[]] have come > to no avail. > > Any help on this is greatly appreciated. > > Roger Williams > Franklin Laboratory > http://www.youtube.com/congruentlight > >
From: Yves Klett on 19 Mar 2010 07:45 Hi, what about: target=b[a[c],a[c],a[d]]; replace={e,f,g}; ReplacePart[target,MapIndexed[{#2[[1]],1}->#&,replace]] b[a[e],a[f],a[g]] ReplacePart may possibly be slow for large alterations (see e.g. http://www.mathprogramming-intro.org/book/node288.html) Regards, Yves Am 19.03.2010 08:47, schrieb divisor: > Hello mathGroup: > > I have an expression like this: > > b[ a[c], a[c], a[d]] > > a list like this: > > {e,f,g} > > I want to end up with > > b[ a[e], a[f], a[g]] > > I think of this as interleaving a list into an expression, but all my > tries with ./,.//,MapIndexed[],MapAt[], Partition[Riffle[]] have come > to no avail. > > Any help on this is greatly appreciated. > > Roger Williams > Franklin Laboratory > http://www.youtube.com/congruentlight >
|
Next
|
Last
Pages: 1 2 3 Prev: blobs functions Next: Function construction and also symmetric matrices |