+ 1

Return all alphabetic words from a list

Hi, I have a list of words and I want return all alphabetic words from this list. It must look like this, but it doesn’t work: import re words = [ “I”, “want”, “,”,”:”, “/“, “Sleep”] for word in words: if word not re.match(r’^[0-9\-\.,]+$’, words): print(word) The out must be: words= [“i”, “want”, “sleep”]

14th Apr 2020, 12:10 PM
Katja
Katja - avatar
9 Antworten
+ 3
Katja, isalpha will give True for everything that only consists of letters.
14th Apr 2020, 12:31 PM
HonFu
HonFu - avatar
+ 3
Mirielle , it retuns me “ str” object has no attribute isAlpha
14th Apr 2020, 12:48 PM
Katja
Katja - avatar
+ 3
Ah, so you want all the words lowered? Yeah, then just write words2 = [w.lower() for w in words if w.isalpha()]
14th Apr 2020, 5:08 PM
HonFu
HonFu - avatar
+ 2
Do you need re for that? Why not just: for word in words: if word.isalpha(): print(word)
14th Apr 2020, 12:17 PM
HonFu
HonFu - avatar
+ 2
If you want to create a new list only with pure words, you can write: words2 = [w for w in words if w.isalpha()]
14th Apr 2020, 12:22 PM
HonFu
HonFu - avatar
+ 2
HonFu , it looks like this: words=[word for word in words if word.isalpha()] words = [word.lower() for word in words] it return exactly what i want✹
14th Apr 2020, 12:43 PM
Katja
Katja - avatar
+ 1
HonFu, yes, but it must also return lower case words
14th Apr 2020, 12:26 PM
Katja
Katja - avatar
0
U can write the code using isalpha().. the code is ... for word in words: If word.isalpha(): print(word)
15th Apr 2020, 10:19 AM
Abhishek
Abhishek - avatar
0
arr = ["i","want",",",":","/","sleep"] words = [] for x in arr: if x.isalpha(): words.append(x) print(words) ####___________________________________ outputs ['i','want','sleep'] Try it and let me know whether it was perfect for you or not. If not i will try something else for you then. but I guess this is the solution
16th Apr 2020, 9:16 AM
CoderBittu
CoderBittu - avatar