جواب «Clean Words Only» چالش 2 از جلسهی 71
سؤال
تابع tokens(text) را کامل کن: هر کلمه را کوچک کن، فقط حرفهای الفبا را نگه دار (isalpha)، و اگر خالی نبود و در STOP نبود، به لیست اضافه کن. خروجی:['sun', 'heats', 'water', 'rises']['evaporation']['rain', 'fall', 'from', 'clouds']
راهنما
w = "".join(ch for ch in w.lower() if ch.isalpha()) و بعد if w and w not in STOP: words.append(w).
یک جواب درست
STOP = {"the", "a", "an", "is", "are", "and", "or", "of", "in", "on", "to", "into", "it", "this",
"that", "what", "how", "why", "does", "do", "as", "when", "they", "called", "its"}
def tokens(text):
words = []
for w in text.lower().split():
w = "".join(ch for ch in w if ch.isalpha())
if w and w not in STOP:
words.append(w)
return words
print(tokens("The Sun heats the water, and it rises!"))
print(tokens("What is evaporation?"))
print(tokens("Why does rain fall from clouds?"))
اول خودت امتحان کن؛ جوابهای دیگر هم قبول است. برگرد و خودت حلش کن