我试图使用Python从字符串中删除特定字符。这是我现在使用的代码。不幸的是,它似乎对字符串没有做任何事情。

for char in line:
    if char in " ?.!/;:":
        line.replace(char,'')

我如何正确地做到这一点?


当前回答

line = line.translate(None, " ?.!/;:")

其他回答

Python中的字符串是不可变的(不能更改)。因此,line.replace(…)的作用只是创建一个新字符串,而不是更改旧字符串。您需要将其重新绑定(赋值)到行,以便使该变量具有新的值,并删除那些字符。

而且,你做的方法相对来说会比较慢。对于有经验的python编程者来说,这也可能会有点困惑,他们会看到一个双嵌套结构,并认为正在发生更复杂的事情。

从Python 2.6和更新的Python 2开始。x版本*,你可以使用str.translate,(见下面的Python 3答案):

line = line.translate(None, '!@#$')

或者用re.sub替换正则表达式

import re
line = re.sub('[!@#$]', '', line)

括号内的字符构成一个字符类。行中属于该类的任何字符都被替换为要sub的第二个参数:空字符串。

Python 3答案

在Python 3中,字符串是Unicode。你得翻译得有点不同。Kevpie在其中一个答案的注释中提到了这一点,str.translate的文档中也提到了这一点。

在调用Unicode字符串的translate方法时,不能传递上面使用的第二个参数。也不能将None作为第一个参数。相反,您传递一个翻译表(通常是字典)作为唯一的参数。这个表将字符的序号值(即对它们调用ord的结果)映射到应该替换它们的字符的序号值,或者对我们来说有用的是none,表示它们应该被删除。

因此,要对Unicode字符串执行上述操作,您可以调用类似于

translation_table = dict.fromkeys(map(ord, '!@#$'), None)
unicode_line = unicode_line.translate(translation_table)

这里使用dict.fromkeys和map简洁地生成包含

{ord('!'): None, ord('@'): None, ...}

甚至更简单,正如另一个答案所说,在适当的地方创建翻译表:

unicode_line = unicode_line.translate({ord(c): None for c in '!@#$'})

或者,正如Joseph Lee提出的那样,使用str.maketrans创建相同的翻译表:

unicode_line = unicode_line.translate(str.maketrans('', '', '!@#$'))

*为了与早期的python兼容,你可以创建一个“null”转换表来代替None:

import string
line = line.translate(string.maketrans('', ''), '!@#$')

这里的字符串。Maketrans用于创建一个转换表,它只是一个包含序数为0到255的字符的字符串。

我使用的方法可能没有那么有效,但它非常简单。我可以一次删除不同位置的多个字符,使用切片和格式化。 这里有一个例子:

words = "things"
removed = "%s%s" % (words[:3], words[-1:])

这将导致在“removed”中保留单词“This”。

格式化对于在打印字符串的中途打印变量非常有用。它可以插入任何数据类型,使用%后跟变量的数据类型;所有数据类型都可以使用%s,浮点数(即小数)和整数可以使用%d。

Slicing can be used for intricate control over strings. When I put words[:3], it allows me to select all the characters in the string from the beginning (the colon is before the number, this will mean 'from the beginning to') to the 4th character (it includes the 4th character). The reason 3 equals till the 4th position is because Python starts at 0. Then, when I put word[-1:], it means the 2nd last character to the end (the colon is behind the number). Putting -1 will make Python count from the last character, rather than the first. Again, Python will start at 0. So, word[-1:] basically means 'from the second last character to the end of the string.

因此,通过切断我想要删除的字符之前的字符和之后的字符,并将它们夹在一起,我可以删除不需要的字符。把它想象成香肠。中间是脏的,所以我想把它处理掉。我只是把我想要的两端剪掉,然后把它们放在一起,中间没有多余的部分。

如果我想删除多个连续字符,我只需在[](切片部分)中移动数字。或者,如果我想从不同的位置删除多个字符,我可以简单地一次将多个切片夹在一起。

例子:

 words = "control"
 removed = "%s%s" % (words[:2], words[-2:])

remove = 'cool'。

words = "impacts"
removed = "%s%s%s" % (words[1], words[3:5], words[-1])

remove = 'macs'。

在本例中,[3:5]表示位置3到位置5的字符(不包括最后位置的字符)。

记住,Python从0开始计数,所以你也需要这样做。

这里有一些可能的方法来完成这个任务:

def attempt1(string):
    return "".join([v for v in string if v not in ("a", "e", "i", "o", "u")])


def attempt2(string):
    for v in ("a", "e", "i", "o", "u"):
        string = string.replace(v, "")
    return string


def attempt3(string):
    import re
    for v in ("a", "e", "i", "o", "u"):
        string = re.sub(v, "", string)
    return string


def attempt4(string):
    return string.replace("a", "").replace("e", "").replace("i", "").replace("o", "").replace("u", "")


for attempt in [attempt1, attempt2, attempt3, attempt4]:
    print(attempt("murcielago"))

附注:在使用" ?.!/;:"的例子中使用元音…是的,“murcielago”在西班牙语里是蝙蝠的意思…有趣的单词,因为它包含了所有的元音:)

PS2:如果你对性能感兴趣,你可以用一个简单的代码来衡量这些尝试:

import timeit


K = 1000000
for i in range(1,5):
    t = timeit.Timer(
        f"attempt{i}('murcielago')",
        setup=f"from __main__ import attempt{i}"
    ).repeat(1, K)
    print(f"attempt{i}",min(t))

在我的盒子里,你会得到:

attempt1 2.2334518376057244
attempt2 1.8806643818474513
attempt3 7.214925774955572
attempt4 1.7271184513757465

因此,对于这个特定的输入,尝试4似乎是最快的。

即使是下面的方法也是有效的

line = "a,b,c,d,e"
alpha = list(line)
        while ',' in alpha:
            alpha.remove(',')
finalString = ''.join(alpha)
print(finalString)

输出:中的

>>> s = 'a1b2c3'
>>> ''.join(c for c in s if c not in '123')
'abc'