如何删除python中字符串中字符的所有实例? [英] How to delete all instances of a character in a string in python?
问题描述
如何删除此字符串中某个字符的所有实例?这是我的代码:
def findreplace(char, string):place = string.index(char)字符串[地点] = ''返回字符串
但是,如果我运行它,就会发生这种情况:
<预><代码>>>>findreplace('i', '天气很冷')回溯(最近一次调用最后一次):文件<stdin>",第 1 行,位于 <module>文件<stdin>",第 3 行,在 findreplace 中类型错误:str"对象不支持项目分配这是为什么?
字符串在 Python 中是不可变的,这意味着一旦创建了字符串,就不能更改字符串的内容.如果您需要更改它,则会使用更改创建字符串的新实例.
考虑到这一点,我们有很多方法可以解决这个问题
使用
<预><代码>>>>"它是冰".replace("i", "")'不是'str.replace
,使用
<预><代码>>>>它是冰冷的".翻译(无,我")'不是'str.translate
,使用正则表达式,
<预><代码>>>>进口重新>>>re.sub(r'i', "", "很冷")'不是'使用理解作为过滤器,
<预><代码>>>>"".join([char for char in "it is icy" if char != "i"])'t 是 cy'使用
<预><代码>>>>"".join(filter(lambda char: char != "i", "它是冰的"))'不是'filter
函数
时间对比
def findreplace(m_string, char):m_string = 列表(m_string)对于 m_string 中的 k:如果 k == 字符:德尔(m_string[m_string.index(k)])返回 "".join(m_string)定义替换(m_string,字符):return m_string.replace("i", "")定义翻译(m_string,字符):返回 m_string.translate(None, "i")从时间导入时间print timeit("findreplace('it is icy','i')", "from __main__ import findreplace")print timeit("replace('it is icy','i')", "from __main__ import replace")print timeit("translate('it is icy','i')", "from __main__ import translate")
结果
1.644745826720.292785882950.311302900314
str.replace
和 str.translate
方法比接受的答案快 8 到 5 倍.
注意:理解方法和过滤方法预计会更慢,对于这种情况,因为它们必须创建列表,然后必须再次遍历以构造一个字符串.而 re
对于单个字符替换来说有点矫枉过正.因此,它们都被排除在时间比较之外.
How do I delete all the instances of a character in this string? Here is my code:
def findreplace(char, string):
place = string.index(char)
string[place] = ''
return string
However, if I run this, this is what happens:
>>> findreplace('i', 'it is icy')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in findreplace
TypeError: 'str' object does not support item assignment
Why is this?
Strings are immutable in Python, which means once a string is created, you cannot alter the contents of the strings. If at all, you need to change it, a new instance of the string will be created with the alterations.
Having that in mind, we have so many ways to solve this
Using
str.replace
,>>> "it is icy".replace("i", "") 't s cy'
Using
str.translate
,>>> "it is icy".translate(None, "i") 't s cy'
Using Regular Expression,
>>> import re >>> re.sub(r'i', "", "it is icy") 't s cy'
Using comprehension as a filter,
>>> "".join([char for char in "it is icy" if char != "i"]) 't s cy'
Using
filter
function>>> "".join(filter(lambda char: char != "i", "it is icy")) 't s cy'
Timing comparison
def findreplace(m_string, char):
m_string = list(m_string)
for k in m_string:
if k == char:
del(m_string[m_string.index(k)])
return "".join(m_string)
def replace(m_string, char):
return m_string.replace("i", "")
def translate(m_string, char):
return m_string.translate(None, "i")
from timeit import timeit
print timeit("findreplace('it is icy','i')", "from __main__ import findreplace")
print timeit("replace('it is icy','i')", "from __main__ import replace")
print timeit("translate('it is icy','i')", "from __main__ import translate")
Result
1.64474582672
0.29278588295
0.311302900314
str.replace
and str.translate
methods are 8 and 5 times faster than the accepted answer.
Note: Comprehension method and filter methods are expected to be slower, for this case, since they have to create list and then they have to be traversed again to construct a string. And re
is a bit overkill for a single character replacement. So, they all are excluded from the timing comparison.
这篇关于如何删除python中字符串中字符的所有实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!