kid_2901 - 6 months ago 16

Python Question

Let's say I have a list of:

`5 10 10 20 50 50 20`

(there are 4 distinguish numbers).

I want to convert them to:

`0 1 1 2 3 3 2`

(then convert back to the original form).

There are tons of ways to do that, but I am not sure what is the best and Pythonic way?

(a way is to generate a set, convert the set to a list, sort the list, then generate output by the sorted list, but I think it is not the best one)

Answer

I think this is a good problem for make use of `collections.defaultdict()`

and `itertools.count()`

methods.

```
from itertools import count
from collections import defaultdict
c = count()
dct = defaultdict(lambda: next(c))
lst = [5, 10, 10, 20, 50, 50, 20]
conv = [dct[i] for i in lst]
# [0, 1, 1, 2, 3, 3, 2]
back = [k for c in conv for k, v in dct.items() if v == c]
# [5, 10, 10, 20, 50, 50, 20]
```

Source (Stackoverflow)

Comments