17
$\begingroup$

Say you have two lists:

listA = {a, b, c}

listB = {d, e, f}

How would you produce a listC that equates each of the two's elements, ie.

listC = {a == d, b == e, c == f} ?

I know, it's very easy, but somehow I've already spent an slightly embarrassing amount of time on this. I suspect it will involve Map, but can't seem to get the elements to behave together per each element. It seems to just Append the two together for some reason.

SetAttributes[listA, Listable] SetAttributes[listB, Listable]

seems promising given this: http://reference.wolfram.com/mathematica/ref/Listable.html

$\endgroup$
7
  • 4
    $\begingroup$ Thread[listA == listB] $\endgroup$
    – rm -rf
    Commented Jun 12, 2013 at 0:45
  • 1
    $\begingroup$ Can you answer your own question now, using rm's hint? $\endgroup$ Commented Jun 12, 2013 at 2:22
  • 1
    $\begingroup$ @rm-rf If listA is equal to listB, then Thread[listA == listB] will return just True. In that case, this: MapThread[Equal, {listA, listB}]. $\endgroup$
    – Michael E2
    Commented Jun 12, 2013 at 5:37
  • 1
    $\begingroup$ Related: Equating matrices (or higher order tensors) element-wise $\endgroup$ Commented Jun 12, 2013 at 7:43
  • 1
    $\begingroup$ Also related: (10211) (15556) $\endgroup$
    – Mr.Wizard
    Commented Jan 18, 2015 at 15:29

2 Answers 2

19
$\begingroup$

As @rm -rf mentioned in the comments,

Thread[listA == listB]

accomplishes what I'd hoped. Apoogies for missing this.

As Michael notes, if they are already equal to these values (you're not newly naming listA or listB) and they happen to already be equal , the query just returns "true." If this is the case, use:

MapThread[Equal, {listA, listB}]

as mentioned by MichaelE2 in the comments.

$\endgroup$
3
  • $\begingroup$ MapThread gives True/False. Any quick way to get them to 1 and 0? $\endgroup$ Commented Nov 26, 2017 at 21:41
  • $\begingroup$ @ChenStatsYu If I understand, you would like "True" to be indicated with a 1 and "False" with a 0? MapThread[Equal, {list1, list2}] /. {True :> 1, False :> 0} achieves this result. If it is something other than True/False, a conditional statement such as MapThread[~] /. {x_ /; x != True :> 0} should work, but doesn't - I think due to the unequal operator. However, some such conditional statement dependent upon the entity being replaced should suffice, or you can use Cases[~] or Position[MapThreads[~], True] to eliminate or detect, respectively, which statements were True. $\endgroup$
    – Ghersic
    Commented Dec 5, 2017 at 17:12
  • 1
    $\begingroup$ @ChenStatsYu Boole does this $\endgroup$
    – b3m2a1
    Commented Nov 17, 2019 at 22:16
4
$\begingroup$

Also

Inner[Equal, listA, listB, List]
Equal @@@ Transpose[{listA, listB}]

{a == d, b == e, c == f}

$\endgroup$
2
  • $\begingroup$ How about setting variable list to equal values list variables = {"a", "b", "c", "d", "e"}, list = {0.745581, -0.412515, 0.701289, 0.666934, -0.559174}, list3 = Thread[variables = list], then IN a give OUT a ? $\endgroup$
    – SPIL
    Commented Nov 14, 2017 at 10:33
  • $\begingroup$ Years late "thanks," kglr. I did +1 back in 2013! SPIL, I'm not exactly sure what you're asking, but if you've a reasonably short list the notebook Frontend can display it all, skipping preliminary list definitions entirely and saying {a, b, c} = {1, 2, 3} will auto-thread Set over each pair. How to do this for sufficiently large lists that they must be represented internally is another question; it is probably doable via Part and Table or maybe Map. If setting a bunch of variables to a bunch of values is what you're after anyways... You will want to remove the quotes though. $\endgroup$
    – Ghersic
    Commented Dec 5, 2017 at 18:48

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.