Duy Nguyen
Duy Nguyen

Reputation: 5

Remove special char out of a word

I am trying to count the word frequency in a long string. I have split the string into list of words using string.split() method and remove case sensity by applying string.lower() before splitting the long string. I want to remove some special character such as '!', ':', '.' as those character will mess up the word count. Below is the function that I wrote but it seems not to work properly

def clean_word(word):
    replace_list = [':','.',',','!','?']
    s = list(word)
    for i in s:
        for j in replace_list:
            if i == j:
                i=""
print(s) # to see s before it being joined           
word =''.join(s)
return word
print(clean_word('Hello!'))

The result is:

['H', 'e', 'l', 'l', 'o', '!']

Hello!

I wonder why "!" has not been replaced with ""? I did put in a test code at line and it shows the comparison works.

   if i == j:
       print('Checked')

Upvotes: 0

Views: 72

Answers (4)

Gsk
Gsk

Reputation: 2945

You should use list comprehension, is faster and cleaner:

replace_list = [':','.',',','!','?']

word = "H:e.l,l!o?"

print ''.join([c for c in word if c not in replace_list]) #OUTPUTS: Hello

Upvotes: 0

Austin
Austin

Reputation: 26039

Use enumerate:

def clean_word(word):
    replace_list = [':','.',',','!','?']
    s = list(word)
    for i, x in enumerate(s):
        if x in replace_list:
            s[i] = ""     
    word = ''.join(s)
    return word

print(clean_word('Hello!'))

# Hello

If you are interested in a list-comprehension:

word = 'Hello!'
replace_list = [':','.',',','!','?']

print(''.join([x for x in word if x not in replace_list]))
# Hello

Upvotes: 3

Druta Ruslan
Druta Ruslan

Reputation: 7412

def clean_word(word):
    replace_list = [':','.',',','!','?']
    new_word = ''
    for x in word:
        if x not in replace_list:
            new_word += x     
    return new_word
print(clean_word('Hello!'))

Output

Hello

Upvotes: 0

Taohidul Islam
Taohidul Islam

Reputation: 5414

It can be solved more easily:

def clean_word(word):
    replace_list = [':','.',',','!','?']
    for i in replace_list:
        word = word.replace(i,"")
    return word

print(clean_word('Hello!'))

Error of your code: in your code you are writing i="", it changes the value of variable i, not the original string.

Upvotes: 0

Related Questions