Search code examples
pythonlistnestedunique

Replace unique values from nested list with numbers in python


How could I replace unique values in nested list with numbers ?

sample = [["P1","P13","P2","P2"],
          ["P2","P13P14","P1","P0","P1"],
          ["P1","P0","P3"],
          ["P17","P3","P15P15"],
          ["P1","P5"]]

from sample I can create a list of unique values:

unique_sample = sorted(list(set(x for l in sample for x in l)))

Desired output is to return index from unique_sample for each value in sample nested list

output = [[4,0,5,5],
          [5,7,4,0,4],
          [4,3,6],
          [8,6,2],
          [4,1]]

Solution

  • Here is one way:

    from itertools import chain
    
    sample = [["P1","P13","P2","P2"],
              ["P2","P13P14","P1","P0","P1"],
              ["P1","P0","P3"],
              ["P17","P3","P15P15"],
              ["P1","P5"]]
    
    d = {j: i for i, j in enumerate(sorted(set(chain(*sample))))}
    
    result = [list(map(d.get, i)) for i in sample]
    
    # [[1, 2, 6, 6],
    #  [6, 3, 1, 0, 1],
    #  [1, 0, 7],
    #  [5, 7, 4],
    #  [1, 8]]