Rachie Rachie - 1 year ago 84
Python Question

Replacing python list elements with key

I have a list of non-unique strings:

list = ["a", "b", "c", "a", "a", "d", "b"]

I would like to replace each element with an integer key which uniquely identifies each string:

list = [0, 1, 2, 0, 0, 3, 1]

The number does not matter, as long as it is a unique identifier.

So far all I can think to do is copy the list to a set, and use the index of the set to reference the list. I'm sure there's a better way though.

Answer Source

This will guarantee uniqueness and that the id's are contiguous starting from 0:

id_s = {c: i for i, c in enumerate(set(list))}
li = [id_s[c] for c in list]

On a different note, you should not use 'list' as variable name because it will shadow the built-in type list.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download