Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 循环以删除一些字符串_Python_Python 3.x - Fatal编程技术网

Python 循环以删除一些字符串

Python 循环以删除一些字符串,python,python-3.x,Python,Python 3.x,我试图删除字符串中的一些字符,但我有一个错误,我有三种不同的方式循环,但它仍然不起作用 我的问题是如何循环字符串并删除字符 这是我的密码: MyList= [",", ":", "\"", "=", "&", ";", "%", "$","@", "%", "^", "*", "(", ")", "{", "}","[", "]", "|", "/", "\\", ">", "<", "-",'!', '?', '.', "'",'--',

我试图删除字符串中的一些字符,但我有一个错误,我有三种不同的方式循环,但它仍然不起作用

我的问题是如何循环字符串并删除字符

这是我的密码:

MyList= [",", ":", "\"", "=", "&", ";", "%", "$","@", "%", "^",
         "*", "(", ")", "{", "}","[", "]", "|", "/", "\\", ">",
         "<", "-",'!', '?', '.', "'",'--', '---', "#"]

for remove in MyList:   

        mystring =re.sub(remove, "", "I am trying this code")
        print (remove)
MyList=[“,”,“:”,“\”,“=”,“&”,“;”,“%,“$”,“@”,“%,“^”,
"*", "(", ")", "{", "}","[", "]", "|", "/", "\\", ">",

“这不需要正则表达式。只需使用
str.replace

>>> MyList= [",", ":", "\"", "=", "&", ";", "%", "$","@", "%", "^",
...          "*", "(", ")", "{", "}","[", "]", "|", "/", "\\", ">",
...          "<", "-",'!', '?', '.', "'",'--', '---', "#"]
>>> mystring = "Helo, world!?"
>>> for s in MyList:
...     mystring = mystring.replace(s, '')
...
>>> mystring
'Helo world'
>>MyList=[“,”,“:”,“\”,“=”,“&”,“;”,“%”,“$”,“@”,“%,“^”,
...          "*", "(", ")", "{", "}","[", "]", "|", "/", "\\", ">",

…“您还可以将列表合并为一个重新表达式:

pattern = re.compile(r"\b(" + "|".join(MyList) + ")\\W")
pattern.sub("", mystring )

已经发布了一些解决方法,但还没有人解释错误。re.sub()的文档说:

re.sub(pattern,repl,string,…)-返回通过替换repl替换字符串中的…pattern而获得的字符串

因此,在遍历了我列表中的一些符号之后,我们得出了以下结论:

re.sub("*", "", "I am trying this code")
因此,您试图将
*
替换为
——但是
*
是正则表达式中使用的“特殊”字符,在这种情况下,
“*”
是非法/无效的正则表达式。
*
是正则表达式中的量词,表示“返回尽可能多的前一个正则表达式的重复”-但该字符串中没有以前的正则表达式

“*”
通常与
一起使用,如在
“*”
中,这意味着尽可能多地匹配任何单个字符(
”)

更新:以下是我如何从字符串中删除符号:

import string

s = "I# am trying th<>is code!"
print(s.translate(None, string.punctuation))
导入字符串
s=“我正在尝试此代码!”
打印(s.translate(无、字符串、标点符号))

如果您的目标只是删除字符串中的所有标点符号,那么一个保留所有其他字符(
和空格)的正则表达式将是:

import re

s = "Hello, world! how are you?"

print(re.sub("[^ \w]","",s))
结果:

Hello world how are you

它确实比循环中的x replace调用(创建尽可能多的字符串)或带有

的正则表达式更有效。这里是一个带有列表理解和连接的答案,不是最优雅的,也不是最快的:

my_list= [",", ":", "\"", "=", "&", ";", "%", "$","@", "%", "^",
         "*", "(", ")", "{", "}","[", "]", "|", "/", "\\", ">",
         "<", "-",'!', '?', '.', "'",'--', '---', "#"]

my_string = "Hello, world!?"

# one liner
my_string = ''.join([c for c in my_string if c not in my_list])


print(my_string)
my_list=[“,”,“:”,“\”,“=”,“&”,“;”,“%”,“$”,“@”,“%”,“^”,
"*", "(", ")", "{", "}","[", "]", "|", "/", "\\", ">",

"看起来您的正则表达式无效。此工具非常有助于确保您的正则表达式有效:为什么不使用
str.replace
?看起来像是XY问题。您的目标是从文本中删除所有标点符号吗?删除单破折号、双破折号和三破折号很奇怪……这行不通,许多符号需要转义。
my_list= [",", ":", "\"", "=", "&", ";", "%", "$","@", "%", "^",
         "*", "(", ")", "{", "}","[", "]", "|", "/", "\\", ">",
         "<", "-",'!', '?', '.', "'",'--', '---', "#"]

my_string = "Hello, world!?"

# one liner
my_string = ''.join([c for c in my_string if c not in my_list])


print(my_string)