我必須編寫一個函數,它需要2個變量,一個句子和一個數字。函數應該返回字符串中等於或大於數字的唯一字的數量。這個例子的結果應該是:計算字符串中最小長度的唯一字
>>> unique_func("The sky is blue and the ocean is also blue.",3)
6
所有我能想到的解決方法是
def unique_func(sentence,number):
sentence_split = sentence.lower().split()
for w in sentence_split:
if len(w) >= number:
現在我不知道如何繼續我的解決方案。誰能幫我?
我喜歡的第一個解決方案,但它會返回7而不是6因爲我相信'藍'和'藍'是兩個不同的單詞。如何解決這個問題? –
@AlexaElis固定 –
@Artsiom Rudzenka使用'string.punctuation',你忘了所有這些:''#'%$''&)(+*/;:= > @ [] \\ _ ^'{} |〜'' – jamylak