let's have list this:
y=[1018, 1018, 1011, 1012, 1013, 1014, 1019, 1019, 1017] what's pythonic way replace each number with
- the lowest unused integer (>=0), if number has not been seen before
- the same integer has been used replace number otherwise
so list becomes:
y=[0, 0, 1, 2, 3, 4, 5, 5, 6] it's not important first element 0, there must unique maximal matching (= assignment) between 2 lists of numbers, i.e. solution:
y=[3, 3, 4, 0, 2, 5, 6, 6, 1] edit: tried loop using find, solution ugly, know there better way it, it's not relevant how bad did :d
the first idea comes mind convert values set() , enumerate() them, store pairs in dict, , use mapping list comprehension create new list:
>>> y=[1018, 1018, 1011, 1012, 1013, 1014, 1019, 1019, 1017] >>> mapping={v:k k,v in enumerate(set(y))} >>> y1=[mapping[y] y in y] >>> y1 [5, 5, 0, 1, 2, 3, 6, 6, 4]
Comments
Post a Comment