5
$\begingroup$

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]

$\endgroup$
  • $\begingroup$ Use MapThread with pure functions: MapThread[#1 /. x -> #2 &, {list1, list2}]. $\endgroup$ – Szabolcs Oct 30 '15 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 '15 at 14:17
10
$\begingroup$
MapThread[#1 /. x -> #2 &, {list1, list2}]
$\endgroup$
8
$\begingroup$

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++]]
$\endgroup$
5
$\begingroup$
MapThread[Function[x, #][#2] &, {list1, list2}]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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