Search code examples
pythondictionarynested-listsdata-conversion

How to convert elements in a list of list according to label map dictionary?


I am trying to convert the elements of a list of list according to label map dictionary. This original list of lists looks like:

original= [['0','1','2'],
           ['0', '0', '0', '0', '0', '0', '0', '1', '2', '0'],
           ['0', '0', '1', '2', '0', '0', '0', '0']]

The label map dictionary that I want to convert the elements in the original according to:

twitter_label_map = {'0':'O', '1':'B_A', '2':'I_A'}

The desired output should be like:

desired = [['O','B_A','I_A'],
           ['O', 'O', 'O', 'O', 'O', 'O', 'O', 'B_A', 'I_A', 'O'],
           ['O', 'O', 'B_A', 'I_A', 'O', 'O', 'O', 'O']]

I have tried doing this:

desired = [[twitter_label_map[old_label] for labels_list in original] for old_label in labels_list]

However this gives me the following error:

NameError: name 'labels_list' is not defined

Thanks in advance!


Solution

  • You are on the right track, but have mixed up the variables in your list comprehension.

    You have nested lists here. Therefore, you should loop through each sublist in original, and then apply the twitter_label_map to each element e in sublist:

    original = [
        ['0', '1', '2'],
        ['0', '0', '0', '0', '0', '0', '0', '1', '2', '0'],
        ['0', '0', '1', '2', '0', '0', '0', '0']
    ]
    
    twitter_label_map = {'0': 'O', '1': 'B_A', '2': 'I_A'}
    
    result = [[twitter_label_map[e] for e in sublist] for sublist in original]
    
    print(result)
    

    Output:

    [['O', 'B_A', 'I_A'], ['O', 'O', 'O', 'O', 'O', 'O', 'O', 'B_A', 'I_A', 'O'], ['O', 'O', 'B_A', 'I_A', 'O', 'O', 'O', 'O']]
    

    The above is equivalent to:

    result = []
    for sublist in original:
        sub = []  # transformed sublist
        for e in sublist:
            sub.append(twitter_label_map[e])
        result.append(sub)
    

    In other words, nested loops, where we append each transformed sublist to an outer result list.