2
$\begingroup$

Suppose that I have a list myList that has more than one level. Here is an example of such a "data structure":

myList = {{10, "c"}, {20, "a"}, {30, "b"}};

Now suppose that I have a list of characters, called charList:

charList = {"a", "b", "c"};

I want to use Position to find the positions of the charList characters in myList. Of course, I can do this manually -- by calling Position for each character in charList:

Position[myList, _?(#[[2]] == "a" &), {1}, Heads -> False]
Position[myList, _?(#[[2]] == "b" &), {1}, Heads -> False]
Position[myList, _?(#[[2]] == "c" &), {1}, Heads -> False]

{{2}}

{{3}}

{{1}}

And, of course, I can shorten this process by using Table to call Position repeatedly:

Table[
 Position[myList, _?(#[[2]] == charList[[i]] &), {1}, Heads -> False], 
{i, 1, Length[charList]}]

{{{2}}, {{3}}, {{1}}}

But, is it possible to shorten the code even more by using Map? (I often find Map more convenient and easier to read than Table.) My initial thought was to do something like this:

Map[
 Position[myList, _?(#[[2]] == # &), {1}, Heads -> False] &,
charList]

but I get undesired output:

{{}, {}, {}}

I think that I have failed to distinguish between the two different # slots, which refer to two different things (they refer to different &s). How can I distinguish the two # so that I can Map Position across charList?

$\endgroup$
4
  • $\begingroup$ Even with Table it can be quite neater Table[Position[myList, {_, i,___}, {1}, Heads -> False], {i, charList}] $\endgroup$
    – Rojo
    Commented Jan 15, 2014 at 22:09
  • 1
    $\begingroup$ Since there is nice answer this is a comment how to deal with those slots ambiguity (not neat, just straightforward) Map[Function[x, Position[myList, _?(#[[2]] == x &), {1}, Heads -> False]], charList] $\endgroup$
    – Kuba
    Commented Jan 15, 2014 at 22:11
  • $\begingroup$ p.s. related $\endgroup$
    – Kuba
    Commented Jan 15, 2014 at 22:19
  • $\begingroup$ Another related question, in fact my own very first question. $\endgroup$
    – C. E.
    Commented Jan 15, 2014 at 22:37

3 Answers 3

5
$\begingroup$

Shorter:

Position[myList, {_, #}] & /@ charList

Since I feel that the answer is a little short, here's another way:

charList /. MapIndexed[Last@# -> First@#2 &, myList]
$\endgroup$
3
$\begingroup$

Using the OP's original form, distinguish the charList parameter using Function:-

myList = {{10, "c"}, {20, "a"}, {30, "b"}};

charList = {"a", "b", "c"};

Map[
 Function[x, Position[myList, _?(#[[2]] == x &), {1}, Heads -> False]],
 charList]

{{{2}}, {{3}}, {{1}}}

$\endgroup$
1
$\begingroup$

Although I think Anon's:

Position[myList, {_, #}] & /@ charList

is the neatest, here is an approach using Sow and Reap:

fun[u_, v_] := 
 Module[{c = 1}, Join @@ Reap[Sow[c++, Last@#] & /@ u, v, Rule][[2]]]

For example:

fun[myList, charList]

yields:

{"a" -> {2}, "b" -> {3}, "c" -> {1}}

or a list with multiple characters, e.g.

test={{10, "c"}, {20, "a"}, {30, "b"}, {1, "a"}};

then

fun[test,charList]

gives:

{"a" -> {2, 4}, "b" -> {3}, "c" -> {1}}

$\endgroup$

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.