Tanmoy
Tanmoy

Reputation: 251

Dictionaries in Python

I have a text file indicating the frequencies like "read 1 dick 1 john 1 book 1 read 1 different 1 a 1 different 1 " i also have a dictionary defined for these words dict={'a':1,'book':2}

I would like to replace the words by their dictionary values. Could anyone let me know how is this done ?

Upvotes: 0

Views: 172

Answers (4)

Sufian Latif
Sufian Latif

Reputation: 13356

That's easy:

text = # your text here
for word in dictionary:
    text = text.replace(word, str(dictionary[word]))

Edit

For the problems regarding substrings, you can use regular expressions:

import re
text = # your text here
for word in dictionary:
    text = re.sub('^|\s' + word + '\s|$', str(dictionary[word]) + ' ', text)

Upvotes: 1

Mark Longair
Mark Longair

Reputation: 468041

You could also use re.sub, but supplying a function as the replacement argument:

import re

frequencies = {'a': 1, 'book': 2}

input_string = "read 1 dick 1 john 1 book 1 read 1 different 1 a 1 different 1 "

def replace_if_found(m):
    word = m.group(1)
    return str(frequencies.get(word, word)) + m.group(2)

print re.sub(r'(\w+)( \d+)', replace_if_found, input_string)

... which gives you the output:

read 1 dick 1 john 1 2 1 read 1 different 1 1 1 different 1

The advantage there is that it's only replacing where you have one or more word characters followed by one or more digits.

Upvotes: 0

John La Rooy
John La Rooy

Reputation: 304443

import re
text = # your text here
dictionary = # your dictionary here (don't call it dict!)
re.sub("\\b.+?\\b", lambda x: str(dictionary.get(*[x.group()]*2)), text)

Upvotes: 1

John La Rooy
John La Rooy

Reputation: 304443

text = # your text here
dictionary = # your dictionary here (don't call it dict!)
' '.join(str(dictionary.get(word, word)) for word in text.split(' '))

Upvotes: 4

Related Questions