我想从字符串中删除像,,!,.,*这样的特殊字符。我正在剥离特殊字符中的单个单词。字符串的其余部分存储在列表中。
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)循环工作正常。其print(a)即打印结果为['<list_iterator object at 0x0000018EBCE9C6D8>', '<list_iterator object at 0x0000018EBCE9C6D8>'....]格式。我希望输出为,Zen,of,Python,by,Tim,Peters,Beautiful,is,better,than,…
发布于 2019-05-16 22:35:57
如果你不介意使用正则表达式,你可以这样做
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))注意:我假设您的文本只包含ASCII字符,因此,像来自德语的Straçe或GesäGes这样的单词是没有问题的。我还假设您想要删除字符-(如在--显而易见),但如果您想保留它,只需将其添加到non_special_characters。
发布于 2019-05-16 21:41:48
在使用next()时,您将获得一个迭代器对象。
使用:
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)发布于 2019-05-16 21:41:48
在您的原始代码中,您将迭代器对象y附加到列表,而不是实际的字符串。
In [50]: type(y)
Out[50]: list_iterator如果要使用此方法,则需要将next(y)附加到实际提供字符串的列表中
s= zenPython.split()
y=iter(s)
a=[]
for i in range(len(s)):
a.append(next(y))
print(a)或者,您可以使用列表理解来迭代单词,将单词按空格拆分,然后剥离所需的字符并从中创建一个新列表
#Split words by whitespace, and strip the characters you want
a= [ item.strip(',!*.') for item in zenPython.split()]
print(a)输出将是
['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']https://stackoverflow.com/questions/56169936
复制相似问题