def wordlist (l: list) -> list:
'''Returns a wordlist without white spaces and punctuation'''
result = []
table = str.maketrans('!()-[]:;"?.,', ' ')
for x in l:
n = x.translate(table)
n = x.strip()
n = x.split()
if n != []:
result.extend(n)
return result
的功能應該是這樣的工作:刪除空格和標點符號從列表
print(wordlist([' Testing', '????', 'function!!']))
應該產生:
['Testing', 'function']
,但我的代碼有上述收益率:
['Testing', '??', 'function!!']
所以我假設我正在做一些事情正確地與代碼去除標點符號 - 我應該在哪裏修復它?任何其他建議,以簡化代碼也將不勝感激(因爲我覺得它有點冗長)。
你確定你想測試'N = []'而不是'N = 「」' –
我到底拿了出來!因爲它沒有任何意義的功能 –