简体   繁体   中英

How to remove a corresponding word in a dictionary from a string?

I have a dictionary and a text:

{"love":1, "expect":2, "annoy":-2}

test="i love you, that is annoying"

I need to remove the words from the string if they appear in the dictionary. I have tried this code:

for k in dict:
    if k in test:
        test=test.replace(k, "")

However the result is:

i you,that is ing

And this is not what I am looking for, as it should not remove "annoy" as a part of the word, the whole word should be evaluated. How can I achieve it?

First, you should not assign names to variables that are also names of builtin in classes, such as dict .

Variable test is a string composed of characters. When you say, if k in test: , you will be testing k to see if it is a substring of test . What you want to do is break up test into a list of words and compare k against each complete word in that list. If words are separated by a single space, then they may be "split" with:

test .split(' ')

The only complication is that it will create the following list:

['i', '', 'you,', 'that', 'is', 'annoying']

Note that the third item still has a , in it. So we should first get rid of punctuation marks we might expect to find in our sentence:

test.replace('.', '').replace(',', ' ').split(' ')

Yielding:

['i', '', 'you', '', 'that', 'is', 'annoying']

The following will actually get rid of all punctuation:

import string
test.translate(str.maketrans('', '', string.punctuation))

So now our code becomes:

>>> import string
>>> d = {"love":1, "expect":2, "annoy":-2}
>>> test="i love you, that is annoying"
>>> for k in d:
...     if k in test.translate(str.maketrans('', '', string.punctuation)).split(' '):
...         test=test.replace(k, "")
...
>>> print(test)
i  you, that is annoying
>>>

You may now find you have extra spaces in your sentence, but you can figure out how to get rid of those.

you can use this:

query = "i love you, that is annoying"
query = query.replace('.', '').replace(',', '')
my_dict = {"love": 1, "expect": 2, "annoy": -2}
querywords = query.split()

resultwords = [word for word in querywords if word.lower() not in my_dict]
result = ' '.join(resultwords)

print(result)
>> 'i you, that is annoying'

If you want to exclude all words without being key sensitive convert all keys in my_dict to lowercase:

my_dict = {k.lower(): v for k, v in my_dict.items()}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM