If I have list1={x^a, x^b, x^c} and list2={1,2,3}, how do I substitute one into the other to get {1^a, 2^b, 3^c} ? ie. substitution of x

Edit: I need help with a more complicated example. Namely this:

If I have list1={e^(u[1]+a[1]), e^(u[2]+a[2]), e^(u[3]+a[3])} and list2={1,2,3}, how do I substitute one into the other to get {e^(1+a[1]), e^(2+a[2]), e^(3+a[3])} ? ie. substitution of u[i]

  • $\begingroup$ Use MapThread with pure functions: MapThread[#1 /. x -> #2 &, {list1, list2}]. $\endgroup$
    – Szabolcs
    Oct 30, 2015 at 13:03
  • $\begingroup$ There are things to do after your question is answered. Its a good idea to stay vigilant for some time, better approaches may come later improving over previous replies. Experienced users may point alternatives, caveats or limitations. New users should test answers before voting and wait 24 hours before accepting the best one. Participation is essential for the site, please come back to do your part. $\endgroup$
    – rhermans
    Nov 6, 2015 at 14:17

3 Answers 3

MapThread[#1 /. x -> #2 &, {list1, list2}]

Response to the edit:

It's easy to adapt my 2nd and 3rd approach below to meet your requirement:

l = list1; l[[;; , 2, 2]] = list2; l

i = 1; list1 /. u[_] :> list2[[i++]]

Original Answer:

Shortest so far:

list2^list1[[;; , 2]]

2nd shortest so far:

l = list1; l[[;; , 1]] = list2; l

If l isn't introduced to keep list1 unaltered, it'll be another shortest one.

3rd shortest so far:

i = 1; list1 /. x :> list2[[i++]]
MapThread[Function[x, #][#2] &, {list1, list2}]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.