-5

I have a list a which contains all the possible values in list b

a = ['foo', 'bar', 'baz']

and

b = ['baz', 'baz', 'foo', 'foo', 'foo', 'bar', 'foo', 'baz']

I'd like to return a list c which has the number of elements found in b where each element is the index of a in for which the value of b can be found.

Example

c = [2, 2, 0, 0, 0, 1, 0, 2]
agf1997
  • 2,668
  • 4
  • 21
  • 36

2 Answers2

1

One-liner:

c = [a.index(x) for x in b]
Matias Cicero
  • 25,439
  • 13
  • 82
  • 154
1
>>> a = ['foo', 'bar', 'baz']
>>> b = ['baz', 'baz', 'foo', 'foo', 'foo', 'bar', 'foo', 'baz']
>>> [a.index(i) for i in b]
[2, 2, 0, 0, 0, 1, 0, 2]
Saket Mittal
  • 3,726
  • 3
  • 29
  • 49