Reputation: 79
I want to remove special characters like ,
,!
,.
,*
from string. I am getting individual words stripped off the special characters. Remaining part of the string is being stored in a list.
zenPython = '''
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
'''
s= zenPython.split()
y=iter(s)
a=[]
for i in range(len(s)):
print(next(y).strip(',!*.'))
a.append(y)
print(a)
loop is working fine. its print(a) that is printing results in ['<list_iterator object at 0x0000018EBCE9C6D8>', '<list_iterator object at 0x0000018EBCE9C6D8>'....]
format. I want output as [The,Zen,of,Python,by,Tim,Peters,Beautiful,is,better,than,...]
Upvotes: 0
Views: 200
Reputation: 31
If you don't mind using regular expressions you can do this
import re
""""
Pattern to Remove all special characters(and spaces) except the
punctuation mark (’) that appear in contractions i.e aren't or it's
"""
non_special_characters = r"[A-Za-z0-9']+"
print(re.findall(non_special_characters,zenPython))
Note: I assumed that your text only contains ASCII characters,so, there are no problems with words like Straße or Gesäß from german. I also assumed that you want to delete the character - (like in --obvious), but if you want to keep it, just add it to non_special_characters.
Upvotes: 2
Reputation: 20490
In your original code, you are appending the iterator object y
to the list, instead of the actual string.
In [50]: type(y)
Out[50]: list_iterator
If you want to use this approach, you would want to append next(y)
to the list which actually gives you the string
s= zenPython.split()
y=iter(s)
a=[]
for i in range(len(s)):
a.append(next(y))
print(a)
Or you can use a list comprehension for iterating over the words, where you split words by whitespace, and strip the characters you want and create a new list out of it
#Split words by whitespace, and strip the characters you want
a= [ item.strip(',!*.') for item in zenPython.split()]
print(a)
The output will be
['The', 'Zen', 'of', 'Python', 'by', 'Tim', 'Peters', 'Beautiful', 'is', 'better', 'than', 'ugly', 'Explicit', 'is', 'better', 'than', 'implicit', 'Simple', 'is', 'better', 'than', 'complex', 'Complex', 'is', 'better', 'than', 'complicated', 'Flat', 'is', 'better', 'than', 'nested', 'Sparse', 'is', 'better', 'than', 'dense', 'Readability', 'counts', 'Special', 'cases', "aren't", 'special', 'enough', 'to', 'break', 'the', 'rules', 'Although', 'practicality', 'beats', 'purity', 'Errors', 'should', 'never', 'pass', 'silently', 'Unless', 'explicitly', 'silenced', 'In', 'the', 'face', 'of', 'ambiguity', 'refuse', 'the', 'temptation', 'to', 'guess', 'There', 'should', 'be', 'one--', 'and', 'preferably', 'only', 'one', '--obvious', 'way', 'to', 'do', 'it', 'Although', 'that', 'way', 'may', 'not', 'be', 'obvious', 'at', 'first', 'unless', "you're", 'Dutch', 'Now', 'is', 'better', 'than', 'never', 'Although', 'never', 'is', 'often', 'better', 'than', 'right', 'now', 'If', 'the', 'implementation', 'is', 'hard', 'to', 'explain', "it's", 'a', 'bad', 'idea', 'If', 'the', 'implementation', 'is', 'easy', 'to', 'explain', 'it', 'may', 'be', 'a', 'good', 'idea', 'Namespaces', 'are', 'one', 'honking', 'great', 'idea', '--', "let's", 'do', 'more', 'of', 'those']
Upvotes: 1
Reputation: 82765
You are getting a iterator object while using next()
.
Use:
zenPython = '''
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
'''
s= zenPython.split()
y=iter(s)
a=[]
for i in range(len(s)):
val = next(y).strip(',!*.')
print(val)
a.append(val)
print(a)
Upvotes: 2