社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

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

Quanti Monati • 5 年前 • 1886 次点击  

以下是实际代码:

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

为什么会这样?

更新 期望输出:

新的,不是新的

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/51338
 
1886 次点击  
文章 [ 6 ]  |  最新文章 5 年前
Shubham Sharma
Reply   •   1 楼
Shubham Sharma    5 年前

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

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'))
Larissa
Reply   •   2 楼
Larissa    5 年前

试试这个

 string.replace(exception_char, exception_chars_dict[exception_char])

更改为

string = string.replace(exception_char, exception_chars_dict[exception_char])
Alexandre B.
Reply   •   3 楼
Alexandre B.    5 年前

你只需要原谅在你的循环中保存价值。这个 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
Ollie
Reply   •   4 楼
Ollie    5 年前

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

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

Marcin Orlowski
Reply   •   5 楼
Marcin Orlowski    5 年前

replace() 不到位:

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

所以你错过了作业:

string = string.replace(exception_char, exception_chars_dict[exception_char])
Devesh Kumar Singh
Reply   •   6 楼
Devesh Kumar Singh    5 年前

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