I have a dictionary with 100 entries like this..
d = {
'entrenched': ['the Internet is firmly entrenched in our lives.'],
'profoundly': ['racism is profoundly entrenched within different social spheres.']
}
I want to loop through the dictionary and replace one of the words in the value based on the key.
The output should look like this:
d = {
'entrenched': ['the Internet is firmly en... in our lives.'],
'profoundly': ['racism is pr... entrenched within different social spheres.']
}
I tried this code
for k,v in d.items():
for word in v:
if word == k:
gapped_word = word[1:] '...'
final_sentence = v.replace(word,gapped_word )
print(final_sentence)
but got no output. I'm also not sure what the final line should be if instead of printing I am replacing the original value with the adjusted one
CodePudding user response:
As each dictionary item maps from a string to a list you should iterate the items in that list. Then, for each of those items, perform the replacing. In addition, notice that you do not have to split the sentence into words, but simply perform the operation over the entire sentence.
final_d = { k: [item.replace(k, k[:2] "...") for item in v] for k, v in d.items()}
Where the value of final_d
is are desired:
{'entrenched': ['the Internet is firmly en... in our lives.'],
'profoundly': ['racism is pr... entrenched within different social spheres.']}
CodePudding user response:
for word in v:
goes by whole sentences; to get word by word, we need to either:- use
for sentence in v:
thenfor word in sentence.split()
; or - use
for sentence in v:
thenif k in sentence
- use
- To assign it back, we need to replace or assign into the list; to get the index to assign into the array, we can use
for i, sentence in enumerate(v)
then laterv[i] = final_sentence
In general, be careful about modifying the dictionary while you're iterating over it; changing an existing entry should be fine, but anything else can mess up the iteration. If you need to make more extensive changes, build a new dictionary with the result (or build a list of changes to apply, then do that in a separate loop).
CodePudding user response:
You can use regex
like below:
import re
d = {
'entrenched': ['the Internet is firmly entrenched in our lives.'],
'profoundly': ['racism is profoundly entrenched within different social spheres.'],
'firm': ['the Internet is firmly entrenched in our lives.'],
'firmly': ['the Internet is firmly entrenched in our lives.'],
}
{k : [re.sub(fr'\b{k}\b', f'{k[:2]}...', s) for s in v] for k,v in d.items()}
Output
{'entrenched': ['the Internet is firmly en... in our lives.'],
'profoundly': ['racism is pr... entrenched within different social spheres.'],
'firm': ['the Internet is firmly entrenched in our lives.'],
'firmly': ['the Internet is fi... entrenched in our lives.']}
CodePudding user response:
Assuming the values for your dictionary will always be a list containing a single string (your sentence). You could use the code below:
d = {key:[value[0].replace(key, key[:2] "...")] for key, value in d.items()}