Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enumerate unique strings in list

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 ints (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]
like image 457
mikhail Avatar asked Mar 23 '23 10:03

mikhail


1 Answers

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)}
like image 67
Ashwini Chaudhary Avatar answered Apr 01 '23 19:04

Ashwini Chaudhary