【Python】【双语文献】列表元素替换(replace)方法实现

瞎扯

好久没更新了,一直在写项目/维护项目和写开发文档,今天且更新一篇笔记。

本文为技术文章,所以提供双语。
This article is about Information Technology, so I provide bilingual edition.

正文/Body

话不多说,直接开门见山。

拦路虎/Problem

Python 编程语言使用者们,你们是否遇到过此问题:如何通过查找旧值来进行替换操作(即不知道索引,只知道旧值)?

Users of the Python programming language, have you ever encountered this problem: how to replace an old value by searching for it (that is, do not know the index, only know the old value)?

总所周知,在 Python 中,字符串(str)有一个便捷的方法:str().replace 。通过对字符串调用这个方法,可是实现在字符串内查找旧值并替换为新值并返回处理好的结果(不修改自身),甚至还可以指定替换个数。

As we known, in Python, there is a protable method in string (str) : str().replace. By calling this method on a string, it can achieve to find the old value and replace it into the new value then return the processed result (it never modify itself), even you can determine the number of times.

然而,list 类并没有提供相关方法。结合各种教程(大多是书),基本只能知道有以下方法:

However, there are not such a method. Combining various articles (Most of them are in books), we can basically only know the following methods

lst = []  # Here are many elements in the list
lst[x] = y  # x is index, y is the new value

这时没有索引(假如没有)的我们只能要么通过 .index(target) 的方法获取,然后再使用上面方法,但是这个方法只能返回第一个索引,后续的获取不了,如需获取还得使上正则模块,相当麻烦。

If we have no index, we can just get it by .index(target) , then use the solution above. But the returned content just included the first index (where the old value first appears) , no more. For get them, we even will use the re lib, that very troublesome.

启发/Inspire

偶然间发现一篇文章,介绍通过列表生成式直接替换所有的方法,发现这样做确实有效但是缺点意思,于是……

I stumbled upon an article that introduced the methods of directly replacing all the target through list generation. I found that this is really effective but it's not fun, so...

骚操作/My Approach

我写了一个继承 list 的类,在其基础上新增了一个 replace 方法,并且命名为 listPlus(属实有点不要脸).

I wrote a class that inherits list , added a replace method on its basis, and named it listPlus (I'm a bit shameless).

使用方法/Usage

导入/Import

直接复制代码或导入模块就可以了。

Copy the code or import the module directly is ok.

代码截图/Code Screenshot

注意:以上只是精简版!

Attention: This just a lite version!

若需文档和打印说明,请于文末获取完整版!

If you need the document and print messages, pls get full version at the end of this article.

调用说明/Call Description

以前我们定义一个列表一般使用以下方法:

Before, we usually define a variable of list by:

lst = []
lst = list()
lst = list([])

如果你需要使用到关于列表的替换方法,请你在需要的地方改用以下方式:

If you need use the replace method for a list, pls use this way instead of old way:

lst = listPlus()

但是注意:由于此类继承了 list ,所以其本质还是个列表,所以还是可以正常调用 list 类原有的 .remove 等方法。

Attention: Because the class inherited the list , so its essence is still a list. You can call the methods in list like .remove on it normally.

replace 方法/replace method

我们定义这样一个列表,它包含了各种类型的元素:

I define a list which includes many types of elements.

lst = listPlus([0, 1, 2, 3, None, True, False, print, "hello"])

现在,我只需要使用赋值的方式将 replace 返回的内容重新赋值给 lst,即可实现替换。

Now, I just need to reassign the content returned by replace to lst in the way of assignment to realize replacement.

lst = lst.replace(None, True)
lst = lst.replace("hello", "你好")
lst = lst.replace(0, 5)
lst = lst.replace(print, max)

# [5, 1, 2, 3, True, True, 5, <built-in function max>, '你好']

注意:为什么 replace 方法返回的是 listPlus 类型?答:如果返回列表,你还能再调用 replace 吗?

Attention: Why does the return type of replace is a listPlus? Answer: If it return a list, then you cann't called replace method.

另外,这个方法支持跨类型替换,如数字 -> 字符串。

In addition, this method supports cross-type substitution, such as number ->string.

最后,由于能力有限,像 __count (字符串类型中的按次数替换内容)的实现方法没做出来。

Finally, due to limited abilities, the implementation method of such as __count (replacing content by times in string type) has not been worked out.

Beta:replace_text

这个方法比较硬核,也易出错。原是其他教程里介绍的方法,但是实现过程之不严谨使其不能万能。

This method is more violent and prone to mistakes. It was originally the method introduced in other tutorials, but the implementation process is not rigorous enough to make it omnipotent.

实现方法:将列表转换为字符串后进行操作(此时调用的是 str().replace ),说到这,你应该知道本方法需传入参数:target 和 value 的类型了吧!没错,就是字符串。

Achievement: Convert the list into a string and then perform the operation (at this time, the call is' str(). replace '). Speaking of this, you should know that this method needs to incoming which type of parameters: target and value! Yes, it is a string.

警告:你最好确保你要替换的内容中不包括英文引号,如实在需要请成对使用,避免替换后的字符串转换失败!

Warning: You'd better ensure that the content you want to replace does not include English quotation marks. Please use them in pairs if necessary to avoid the string conversion failure after replacement!

使用示例/Example

lst = lst.replace_text('0', '5')

END

获取模块文件/Get the module file.

All of links used CN website. If you are not in CN and you can't visit them, pls use proxy or VPN.

Gitee is a website similar to Github.

Source url
Online(Gitee, 【Recommended】) https://gitee.com/gkcoll/mydiys/blob/master/listPlus.py
网盘(夸克)/Netdisk(Quark) https://pan.quark.cn/s/3403de4c259e
版权声明
作者:灰尘疾客
链接:https://www.gkcoll.xyz/449.html
来源:极客藏源
注意:若文章头部无特殊声明,则文章为作者原创,版权归作者所有,未经允许请勿转载(默认允许)。
转载须知:若无特殊说明,本站所有文章均遵循 CC BY-NC-SA 4.0 协议发布,转载、二次创作请遵守该协议内容,并参考以下文案添加转载声明:本文转载自网站 极客藏源 的用户 @灰尘疾客 的文章 《【Python】【双语文献】列表元素替换(replace)方法实现》。本转载文发布遵循与原文相同的 CC BY-NC-SA 4.0 协议
THE END
分享
二维码
海报
【Python】【双语文献】列表元素替换(replace)方法实现
瞎扯 好久没更新了,一直在写项目/维护项目和写开发文档,今天且更新一篇笔记。 本文为技术文章,所以提供双语。 This article is about Information Technolo……
<<上一篇
下一篇>>