Python在一个句子中清理单词

Pre*_*ter 0 python string python-2.7

我正在尝试编写一个接受字符串(句子)的函数,然后清除它并返回所有字母,数字和一个超级.但是代码似乎有误.请知道我在这里做错了什么.

例如:Blake D'Souza是一个!d!0t
应该返回:Blake D'Souza是一个d0t

蟒蛇:

def remove_unw2anted(str):
    str = ''.join([c for c in str if c in 'ABCDEFGHIJKLNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890\''])
    return str

def clean_sentence(s):
    lst = [word for word in s.split()]
    #print lst
    for items in lst:
        cleaned = remove_unw2anted(items)
    return cleaned

s = 'Blake D\'souza is an !d!0t'
print clean_sentence(s)
Run Code Online (Sandbox Code Playgroud)

Don*_*Don 5

你只返回最后一个清理过的字!

应该:

def clean_sentence(s):
    lst = [word for word in s.split()]

    lst_cleaned = []
    for items in lst:
        lst_cleaned.append(remove_unw2anted(items))
    return ' '.join(lst_cleaned)
Run Code Online (Sandbox Code Playgroud)

一个更短的方法可能是这样的:

def is_ok(c):
    return c.isalnum() or c in " '"

def clean_sentence(s):
    return filter(is_ok, s)

s = "Blake D'souza is an !d!0t"
print clean_sentence(s)
Run Code Online (Sandbox Code Playgroud)