Will W - 8 months ago 31

Python Question

My problem is this.

These are the two lists

`codes = ['a', 'b', 'c', 'a', 'e', 'f', 'g', 'a', 'i', 'j', 'a', 'l']`

pas = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

How would I find the position of all the 'a' in the codes list. And then print out the corresponding item in the pas list.

This is what the output should be. They should also be sorted with the .sort() function.

`1`

4

8

11

I have come up with this code. (That doesnt work)

`qwer = [i for i,x in enumerate(codes) if x == common]`

qwe = [qwer[i:i+1] for i in range(0, len(qwer), 1)]

print(pas[qwe])

What would be the best way to get the correct output?

Answer

```
>>> pas = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
>>> codes = ['a', 'b', 'c', 'a', 'e', 'f', 'g', 'a', 'i', 'j', 'a', 'l']
>>> result = sorted(i for i,j in zip(pas,codes) if j=='a')
>>> for i in result:
... print i
...
1
4
8
11
```