Replacing python list items with key

I have a list of unique lines:

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

I would like to replace each element with an integer key that uniquely identifies each row:

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

A number does not matter if it is a unique identifier.

For now, all I can do is copy the list into a set and use the set index to reference the list. I am sure there is a better way.

+4
source share
5 answers

This ensures uniqueness and that id are adjacent, starting with 0:

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

In another note, you should not use 'list'as a variable name, because it obscures the built-in type list.

+10
source

defaultdict:

from collections import defaultdict
seen = defaultdict()
seen.default_factory = lambda: len(seen)  # you could instead bind to seen.__len__

In [11]: [seen[c] for c in list]
Out[11]: [0, 1, 2, 0, 0, 3, 1]

, !


@user2357112 /, itertools.count. :

from itertools import count
seen = defaultdict(count().__next__)  # .next in python 2

, default_factory seen .

+5
>>> lst = ["a", "b", "c", "a", "a", "d", "b"]
>>> nums = [ord(x) for x in lst]
>>> print(nums)
[97, 98, 99, 97, 97, 100, 98]
+4

, -: . , , :

li = ["a", "b", "c", "a", "a", "d", "b"]
li = map(hash, li)                # Turn list of strings into list of ints
li = [hash(item) for item in li]  # Same as above
+2

:

l = ["a", "b", "c", "a", "a", "d", "b", "abc", "def", "abc"]
from itertools import count
from operator import itemgetter

mapped = itemgetter(*l)(dict(zip(l, count())))

:

from itertools import count

def uniq_ident(l):
    cn,d  = count(), {}
    for ele in l:
        if ele not in d:
            c = next(cn)
            d[ele] = c
            yield c
        else:
            yield d[ele]


In [35]: l = ["a", "b", "c", "a", "a", "d", "b"]

In [36]: list(uniq_ident(l))
Out[36]: [0, 1, 2, 0, 0, 3, 1]
+1

All Articles