Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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_String - Fatal编程技术网

如何在字符列表中循环并用“替换”替换它们_&引用;用Python?

如何在字符列表中循环并用“替换”替换它们_&引用;用Python?,python,string,Python,String,我试图让我的Python程序循环遍历我在列表中编写的不需要的字符列表。我希望它使用for函数将这些字符替换为字符串中的“\u1” global string global OddChars string = "Parenthesis: () Brackets: []" OddChars = ["[", "]", "(", ")", " "] for Replace in OddChars: string = string.replace(Replace, "_") print (

我试图让我的Python程序循环遍历我在列表中编写的不需要的字符列表。我希望它使用for函数将这些字符替换为字符串中的“\u1”

global string
global OddChars
string = "Parenthesis: () Brackets: []"
OddChars = ["[", "]", "(", ")", " "]

for Replace in OddChars:
       string = string.replace(Replace, "_")
print (string)
我希望新字符串具有u而不是()[]和空格。 当我尝试这个方法时,它不起作用,它会打印与以前相同的字符串

>>> s = "Parenthesis: () Brackets: []"
>>> o = ["[", "]", "(", ")", " "]
>>> "".join(["_" if c in o else c for c in s])
'Parenthesis:____Brackets:___'
  • 只需在程序中导入regex模块
  • 如果不想重写列表,请小心在列表中的字符之前使用escape-caracter\,并将其转换为字符串以进行字符串替换。只需在正则表达式中添加“|\”。join(OddChar)和OR选项,但在您的示例中不必这样做
  • 只需使用子模块re.sub, 提示:如果想要更好的性能,可以编译表达式 通过使用re.compile
  • 进口稀土

    OddChar2str=OddChar.join(“|\”)

    您的oddchar现在相当于“\(\\)\\[\\]”

    replace=re.sub(OddChar2str,'.'字符串)


    对于这种类型的操作,请使用
    str.translate
    方法

    Python 2 在Python 2中,我们使用
    string
    模块的
    maketrans
    函数来创建翻译表。(请确保不要覆盖
    string
    模块,我已将变量名
    string
    更改为下面的
    mystring
    。)

    Python 3 在Python 3中,
    string
    模块没有此功能,它已作为静态方法移动到内置的
    str
    (这意味着您可以从
    str
    或字符串实例调用它,如下面的
    my_string
    ):

    函数可以如下所示:

    def replace_chars(my_string, old_chars, new_chars):
        return my_string.translate(str.maketrans(old_chars, new_chars))
    

    我试图纠正你的解决办法。你的逻辑有点不正确。在第一次替换之后,您没有在迭代中传递其他奇数字符的更新字符串。

    这对我在空闲状态下有效,没有
    global
    声明。我认为您存在范围问题。
    全局字符串是在函数外部还是在函数内部?向我们展示在运行时演示问题的可运行代码。是的,我现在就开始工作了。我想我的空闲时间太长了。@user3024151我下面的答案对你有用吗?或者
    “”。join(如果我不在OddChars中,那么我在list(string)中为I添加“u”)
    声明性的,就像python一样,我建议扩展你的答案以包含一些解释性的英文文本。在Python3中,你不
    导入string
    。只需使用
    str.maketrans()
    代替即可。感谢@gnibbler让我注意到这一点。我现在已经谈到这个问题。
    import string
    
    def replace_chars(my_string, old_chars, new_chars):
        translation_table = string.maketrans(old_chars, new_chars)
        return my_string.translate(translation_table)
    
    >>> my_string = "Parenthesis: () Brackets: []"
    >>> translation_table = str.maketrans('[]() ', '_____') # str could be my_string 
    >>> translation_table
    {40: 95, 41: 95, 91: 95, 93: 95, 32: 95}
    >>> my_string.translate(translation_table)
    'Parenthesis:____Brackets:___'
    
    def replace_chars(my_string, old_chars, new_chars):
        return my_string.translate(str.maketrans(old_chars, new_chars))
    
    >>> input_string = "Parenthesis: () Brackets: []"
    >>> temp_string = ''
    >>> odd_chars = ["[", "]", "(", ")", " "]
    >>> for odd_char in odd_chars:
    ...     temp_string = input_string.replace(odd_char, "_")
    ...     input_string = temp_string
    ... 
    >>> print input_string
    Parenthesis:____Brackets:___