Disclaimer: I'm not an experienced Python user.
I encountered a task and now I'm trying to figure out the most elegant way to do it in Python.
Here's the task itself: given a list
of strings return a list of int
s (each int
from 0 to N - 1, where N is the number of unique strings in the list), where each int corresponds to a certain string from initial list. Same strings should be mapped to same numbers, different strings - to different numbers.
The first thing I came up with seems "a little bit" overcomplicated:
a = ["a","b","a","c","b","a"]
map(lambda x: dict(map(lambda x: reversed(x), enumerate(set(a))))[x], a)
The result of code above:
[0, 2, 0, 1, 2, 0]
You can use dict and list comprehensions:
>>> a = ["a","b","a","c","b","a"]
>>> d = {x:i for i, x in enumerate(set(a))}
>>> [d[item] for item in a]
[0, 2, 0, 1, 2, 0]
To preserve order:
>>> seen = set()
>>> d = { x:i for i, x in enumerate(y for y in a
if y not in seen and not seen.add(y))}
>>> [d[item] for item in a]
[0, 1, 0, 2, 1, 0]
The above dict comprehension is equivalent to:
>>> seen = set()
>>> lis = []
for item in a:
if item not in seen:
seen.add(item)
lis.append(item)
...
>>> lis
['a', 'b', 'c']
>>> d = {x:i for i,x in enumerate(lis)}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With