+ 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â]
9 Antworten
+ 3
Katja, isalpha will give True for everything that only consists of letters.
+ 3
Mirielle , it retuns me â strâ object has no attribute isAlpha
+ 3
Ah, so you want all the words lowered?
Yeah, then just write
words2 = [w.lower() for w in words if w.isalpha()]
+ 2
Do you need re for that? Why not just:
for word in words:
if word.isalpha():
print(word)
+ 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()]
+ 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âš
+ 1
HonFu, yes, but it must also return lower case words
0
U can write the code using isalpha().. the code is ...
for word in words:
If word.isalpha():
print(word)
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