代码之家  ›  专栏  ›  技术社区  ›  Quanti Monati

为什么replace()在我的Python函数中不起作用?[副本]

  •  1
  • Quanti Monati  · 技术社区  · 6 年前

    以下是实际代码:

    def replace_exception_chars(string):
        exception_chars_dict = {'Old': 'New', 'old': 'new'}
        exception_chars_keys = list(exception_chars_dict.keys())
        for exception_char in exception_chars_keys:
            if exception_char in string:
                string.replace(exception_char, exception_chars_dict[exception_char])
        return string
    
    print(replace_exception_chars('Old, not old'))
    

    如果我试图运行它,我将在输出中获得未更改的源字符串。请看一下: enter image description here

    为什么会这样?

    更新 期望输出:

    新的,不是新的

    6 回复  |  直到 6 年前
        1
  •  1
  •   Devesh Kumar Singh    6 年前

    replace 不是就地方法,而是返回新字符串,因此需要将结果分配给新字符串。

    从文档中: https://docs.python.org/3/library/stdtypes.html#str.replace

    str.replace(旧的,新的[,计数])
    返回字符串的副本,所有出现的子字符串old替换为new。如果给定了可选参数计数,则只替换出现的第一个计数。

    如果同时迭代键和值,您的逻辑也可以简化很多,如下所示

    def replace_exception_chars(string):
        exception_chars_dict = {'Old': 'New', 'old': 'new'}
    
        #Iterate over key and value together
        for key, value in exception_chars_dict.items():
            #If key is found, replace key with value and assign to new string
            if key in string:
                string = string.replace(key, value)
    
        return string
    
    print(replace_exception_chars('Old, not old'))
    

    输出将是

    New, not new
    
        2
  •  3
  •   Marcin Orlowski    6 年前

    replace() 不到位:

    方法replace() 返回字符串的副本,其中old的出现已替换为new ,可选地将替换数量限制为最大。

    所以你错过了作业:

    string = string.replace(exception_char, exception_chars_dict[exception_char])
    
        3
  •  1
  •   Ollie Brian    6 年前

    功能 str.replace() 不改变调用它的字符串-它不能,字符串是不可变的-它返回新字符串。不将输出保存到变量,因此它将丢失。

    你需要交换 string.replace(exception_char, exception_chars_dict[exception_char]) 对于 string = string.replace(exception_char, exception_chars_dict[exception_char]) .

        4
  •  1
  •   Alexandre B. Raju Kapadne    6 年前

    你只需要原谅在你的循环中保存价值。这个 replace 方法返回字符串。

    def replace_exception_chars(string):
        exception_chars_dict = {'Old': 'New', 'old': 'new'}
        exception_chars_keys = list(exception_chars_dict.keys())
        for exception_char in exception_chars_keys:
            if exception_char in string:
                string = string.replace(
                    exception_char, exception_chars_dict[exception_char])
        return string
    
    
    print(replace_exception_chars('Old, not old'))
    # New, not new
    
        5
  •  1
  •   Larissa    6 年前

    试试这个

     string.replace(exception_char, exception_chars_dict[exception_char])
    

    更改为

    string = string.replace(exception_char, exception_chars_dict[exception_char])
    
        6
  •  0
  •   Shubham Sharma    6 年前

    您需要存储正在存储的值。 所以不是

    string.replace(exception_char, exception_chars_dict[exception_char])
    

    string = string.replace(exception_char, exception_chars_dict[exception_char])
    

    完整代码

    def replace_exception_chars(string):
        exception_chars_dict = {'Old': 'New', 'old': 'new'}
        exception_chars_keys = list(exception_chars_dict.keys())
        for exception_char in exception_chars_keys:
            if exception_char in string:
                string = string.replace(exception_char, exception_chars_dict[exception_char])
        return string
    
    print(replace_exception_chars('Old, not old'))