dontcare
dontcare

Reputation: 33

How to replace some characters of a list of tuples

I have this list of tuples (POS tag) and I need to change some characters, only if they are in the second element os the tuple:

For example:

x = [('We', 'PRP'), ("'re", 'VBP'), ('really', 'RB$'), ('sorry', 'JJ'), ('...', ':')]

I need to change the "strange" character of the second element, in this example: RB$ and :.

I have tried:

x_2[x.index(':')] = 'Dts'

and

x_2[x_2.index[,('$')]] = 'S'

I expect this output:

x_2 = [('We', 'PRP'), ("'re", 'VBP'), ('really', 'RBS'), ('sorry', 'JJ'), ('...', 'Dts')]

Thanks in advance and sorry if it is a really basic question, I pretty new with python.

Upvotes: 3

Views: 4021

Answers (5)

amanb
amanb

Reputation: 5473

First, it is important to understand that tuples are immutable and you should not attempt to modify its contents. It is recommended to convert x into a dict like this, so that you can modify the values of the dict because dicts are mutable in Python.

In [36]: y = dict(x)

In [37]: y
Out[37]: {'We': 'PRP', "'re": 'VBP', 'really': 'RB$', 'sorry': 'JJ'}

Now, you can store all symbols in a variable and look for them in the dict y. Whenever, a symbol is found, just replace it with '' a null value.

In [38]: symbols = '$:;?'
In [39]: for k,v in y.items():
    ...:     for symbol in symbols:
    ...:         if symbol in v:
    ...:             v = v.translate({ord(symbol):''})
    ...:             y[k] = v

In [40]: y
Out[40]: {'We': 'PRP', "'re": 'VBP', 'really': 'RB', 'sorry': 'JJ'}

Let's add another item to y with a special symbol defined in symbols:

In [41]: y['test'] = 'ZZ;'

In [42]: y
Out[42]: {'We': 'PRP', "'re": 'VBP', 'really': 'RB', 'sorry': 'JJ', 'test': 'ZZ;'}

So, if I enclose the above for loop code in a function modify_dict and call, the value for the test element will also be modified:

In [45]: modify_dict()

In [46]: y
Out[46]: {'We': 'PRP', "'re": 'VBP', 'really': 'RB', 'sorry': 'JJ', 'test': 'ZZ'}

To convert it back to a list of tuples:

In [55]: z = [(k,v) for k,v in y.items()]

In [56]: z
Out[56]:
[('We', 'PRP'),
 ("'re", 'VBP'),
 ('really', 'RB'),
 ('sorry', 'JJ'),
 ('test', 'ZZ')]

Upvotes: 0

yatu
yatu

Reputation: 88276

You could use a translation table. In Python 3, you can use the maketrans method from the str class:

change = str.maketrans({"$": "S", ":": "Dts"})

Which allows you to map the values in a string with the translation table by calling translate:

[(i, j.translate(change)) for i,j in x]
# [('We', 'PRP'), ("'re", 'VBP'), ('really', 'RBS'), ('sorry', 'JJ'), ('...', 'Dts')]

Upvotes: 2

hellmean
hellmean

Reputation: 131

one way of doing this:

a, b = zip(*x) # unzip into two lists
b = list(b) # make b a list, not a tuple, in order to be mutable

'''
change values
'''
b[b.index(':')] = 'Dts'
b[b.index[,('$')]] = 'S'

x = list(zip(a,b)) # zip back into an original looking list

Upvotes: 0

modesitt
modesitt

Reputation: 7210

I am not sure why such strings should be changed so I do not know the logic for changing them, but I would just keep a dictionary of what needs to be changed (unless there is different logic and many more strings need be changed than just in this example)

to_change = {
    ':': 'Dts', 
    'RB$': 'RBS'
}

and then change them

x_2 = [(f, to_change.get(s, s)) for f,s in x]

Upvotes: 1

Arkistarvh Kltzuonstev
Arkistarvh Kltzuonstev

Reputation: 6935

Try this :

x1 = [(i,j.replace('$','S').replace(':','Dts')) for i,j in x]

OUTPUT :

[('We', 'PRP'), ("'re", 'VBP'), ('really', 'RBS'), ('sorry', 'JJ'), ('...', 'Dts')]

Upvotes: 0

Related Questions