psagrera
psagrera

Reputation: 141

How to get the same value for duplicates values in a list

I have 2 list (keywords, a):

keywords = ['a','b','c','d','j','i','k']

a = ['a','b','c','d','e','f','a','b','c','d','e','f'] 

What I need to achieve is that the values that are not in the keywords list in list a are replaced with var_x, but identical values must have the same value

Example:

a = ['a','b','c','d','e','f','a','b','c','d','e','f'] ---> ['a','b','c','d','var_1','var_2','a','b','c','d','var_1','var_2']

The problem I am having is that equal values have different values of var_x.

keywords = ['a','b','c','d','j','i','k']
a = ['a','b','c','d','e','f','a','b','c','d','e','f']

def replace_sub_b(sub_b):
    bl = sub_b.split()
    print(bl)
    index = 0
    rl = []
    for c in bl:
        if c in keywords:
            rl.append(c)
        else:
            index += 1
            rl.append(f'var_{index}')
    return ' '.join(rl)

new_b = [replace_sub_b(sub_b) for sub_b in a]

new_b
['a', 'b', 'c', 'd', 'var_1', 'var_1', 'a', 'b', 'c', 'd', 'var_1', 'var_1']

and it should be: ['a', 'b', 'c', 'd', 'var_1', 'var_2', 'a', 'b', 'c', 'd', 'var_2', 'var_1']

Could you give me some clue or tip?

Upvotes: 1

Views: 87

Answers (1)

blhsing
blhsing

Reputation: 106445

You can use a mapping dict to keep track of the name that each non-keyword maps to:

mapping = {}
for i, v in enumerate(a):
    if v not in keywords:
        if v not in mapping:
            mapping[v] = f'var_{len(mapping) + 1}'
        a[i] = mapping[v]

so that with your sample input, a becomes:

['a', 'b', 'c', 'd', 'var_1', 'var_2', 'a', 'b', 'c', 'd', 'var_1', 'var_2']

Upvotes: 2

Related Questions