I have a list of strings. I want to assign a unique number to each string (the exact number is not important), and create a list of the same length using these numbers, in o
If you have k different values, this maps them to integers 0 to k-1 in order of first appearance:
k
0
k-1
>>> names = ['b', 'c', 'd', 'c', 'b', 'a', 'b'] >>> tmp = {} >>> [tmp.setdefault(name, len(tmp)) for name in names] [0, 1, 2, 1, 0, 3, 0]