简体   繁体   English

如何从python中的字符串列表中去除多个不需要的字符?

[英]How to strip multiple unwanted characters from a list of strings in python?

I have the following input string:我有以下输入字符串:

text='''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!'''

So far, I have split the text string into a list like so:到目前为止,我已将text字符串拆分为一个list如下所示:

list=['Although', 'never', 'is', 'often', 'better', 'than', '*right*', 'now.\n\nIf', 'the', 'implementation', 'is', 'hard', 'to', 'explain,', "it's", 'a', 'bad', 'idea.\n\nIf', 'the', 'implementation', 'is', 'easy', 'to', 'explain,', 'it', 'may', 'be', 'a', 'good', 'idea.\n\nNamespaces', 'are', 'one', 'honking', 'great','idea', '--', "let's", 'do', 'more', 'of', 'those!']

Now, I want to use strip function to remove unwanted characters such as \\n\\n and -- from the above list.现在,我想使用strip函数从上面的列表中删除不需要的字符,例如\\n\\n--

Can You please help me on this??你能帮我解决这个问题吗??

Use re module, re.sub function will allow you to do that.使用re模块, re.sub函数将允许您这样做。 We need to replace multilpe \\n occurences with single \\n and remove -- string我们需要更换multilpe \\n单OCCURENCES \\n和删除--

import re

code='''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!'''


result = re.sub('\n{2,}', '\n', code)
result = re.sub(' -- ', ' ', result)

print(result)

After that split() your text.之后 split() 你的文字。

This will split the string with either space or newline character这将使用空格或换行符拆分字符串

import re

output = [i for i in re.split(r'\s|\n{1:2}|--', code) if i]

You can use list comprehension to get rid of --您可以使用列表理解来摆脱--

>>> code='''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!'''
>>> 
>>> [word for word in code.split() if word != '--']
['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!']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM