2

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?

Andrew
  • 10,569
  • 5
  • 51
  • 104

3 Answers3

5

Shorter:

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

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

charList /. MapIndexed[Last@# -> First@#2 &, myList]
C. E.
  • 70,533
  • 6
  • 140
  • 264
3

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}}}

Chris Degnen
  • 30,927
  • 2
  • 54
  • 108
1

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}}

ubpdqn
  • 60,617
  • 3
  • 59
  • 148