4
$\begingroup$

I have a list a = {-1, 2/3, -2 - 4 I, -2 + 7 I, 2 I} and when I use the Position[a, I] it returns {} which is what one should expect.

However, when I create another list c = Join[a, {1 + I Sqrt[3], I}] and then do Position[c, I] I get this result {{6, 2, 1}, {7}}. Which at first glance can be very strange because what Mathematica does here is to apply the Position function on FullForm[c], that is, Apply[Position[#, I] &, FullForm[c]].

Is there a way to make Position[a, I] to only return {{7}} and not {{6, 2, 1}, {7}}? I know one can do Last[Position[a, I]] which will give the required answer, however this is does not work in general since I can occur anywhere in the list c.

Any ideas will be much appreciated.

$\endgroup$
8
$\begingroup$

How about

Position[c, I, {1}]

or

Position[N@c, N@I]

Then I'd like to point out that, your explanation "what Mathematica does here is to apply the Position function on FullForm[c]" is incorrect, because FullForm always represents the essence of the code, Position is just trying to find the FullForm of the pattern in the FullForm of the list, it has done nothing strange on c:

I // FullForm
(* Complex[0, 1] *)
a // FullForm
(* List[-1, Rational[2, 3], Complex[-2, -4], Complex[-2, 7], Complex[0, 2]] *)
{1 + I Sqrt[3], I} // FullForm
(* List[Plus[1, Times[Complex[0, 1], Power[3, Rational[1, 2]]]], Complex[0, 1]] *)
$\endgroup$
  • $\begingroup$ This is exactly what I meant. Sorry for the miscommunication. Of course its because there is no Complex[0, 1] in the list a that is why Position[a, I] returns {}. $\endgroup$ – saintdoe Nov 13 '15 at 13:49

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.