问题:从Python中的字符串中删除特定字符
我正在尝试使用Python从字符串中删除特定字符。这是我现在正在使用的代码。不幸的是,它似乎对字符串没有任何作用。
for char in line:
if char in " ?.!/;:":
line.replace(char,'')
如何正确执行此操作?
I’m trying to remove specific characters from a string using Python. This is the code I’m using right now. Unfortunately it appears to do nothing to the string.
for char in line:
if char in " ?.!/;:":
line.replace(char,'')
How do I do this properly?
回答 0
Python中的字符串是不可变的(无法更改)。因此,的效果line.replace(...)
只是创建一个新字符串,而不是更改旧字符串。您需要重新绑定(分配)它line
,以使该变量采用新值,并删除这些字符。
而且,相对而言,您的操作方式会比较缓慢。这也可能会使经验丰富的pythonator感到有些困惑,他们将看到双重嵌套的结构,并暂时认为会发生一些更复杂的事情。
从Python 2.6和更高版本的Python 2.x版本*开始,您可以改用str.translate
,(但请继续阅读Python 3的不同之处):
line = line.translate(None, '!@#$')
或将正则表达式替换为 re.sub
import re
line = re.sub('[!@#$]', '', line)
方括号内的字符构成一个字符类。line
该类中的所有字符都被替换为第二个参数sub
:空字符串。
在Python 3中,字符串是Unicode。您必须进行一些不同的翻译。kevpie在对其中一个答案的评论中提到了这一点,并在的文档中str.translate
对此进行了注明。
当调用translate
Unicode字符串的方法时,您不能传递上面使用的第二个参数。您也不能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 '!@#$'})
或使用创建相同的翻译表str.maketrans
:
unicode_line = unicode_line.translate(str.maketrans('', '', '!@#$'))
*为了与早期的Python兼容,您可以创建一个“空”转换表来代替None
:
import string
line = line.translate(string.maketrans('', ''), '!@#$')
这string.maketrans
是用来创建转换表的,它只是一个字符串,其中包含序号为0到255的字符。
Strings in Python are immutable (can’t be changed). Because of this, the effect of line.replace(...)
is just to create a new string, rather than changing the old one. You need to rebind (assign) it to line
in order to have that variable take the new value, with those characters removed.
Also, the way you are doing it is going to be kind of slow, relatively. It’s also likely to be a bit confusing to experienced pythonators, who will see a doubly-nested structure and think for a moment that something more complicated is going on.
Starting in Python 2.6 and newer Python 2.x versions *, you can instead use str.translate
, (but read on for Python 3 differences):
line = line.translate(None, '!@#$')
or regular expression replacement with re.sub
import re
line = re.sub('[!@#$]', '', line)
The characters enclosed in brackets constitute a character class. Any characters in line
which are in that class are replaced with the second parameter to sub
: an empty string.
In Python 3, strings are Unicode. You’ll have to translate a little differently. kevpie mentions this in a comment on one of the answers, and it’s noted in the documentation for str.translate
.
When calling the translate
method of a Unicode string, you cannot pass the second parameter that we used above. You also can’t pass None
as the first parameter. Instead, you pass a translation table (usually a dictionary) as the only parameter. This table maps the ordinal values of characters (i.e. the result of calling ord
on them) to the ordinal values of the characters which should replace them, or—usefully to us—None
to indicate that they should be deleted.
So to do the above dance with a Unicode string you would call something like
translation_table = dict.fromkeys(map(ord, '!@#$'), None)
unicode_line = unicode_line.translate(translation_table)
Here dict.fromkeys
and map
are used to succinctly generate a dictionary containing
{ord('!'): None, ord('@'): None, ...}
Even simpler, as another answer puts it, create the translation table in place:
unicode_line = unicode_line.translate({ord(c): None for c in '!@#$'})
Or create the same translation table with str.maketrans
:
unicode_line = unicode_line.translate(str.maketrans('', '', '!@#$'))
* for compatibility with earlier Pythons, you can create a “null” translation table to pass in place of None
:
import string
line = line.translate(string.maketrans('', ''), '!@#$')
Here string.maketrans
is used to create a translation table, which is just a string containing the characters with ordinal values 0 to 255.
回答 1
我是否在这里遗漏了要点,或者只是以下内容:
string = "ab1cd1ef"
string = string.replace("1","")
print string
# result: "abcdef"
将其循环:
a = "a!b@c#d$"
b = "!@#$"
for char in b:
a = a.replace(char,"")
print a
# result: "abcd"
Am I missing the point here, or is it just the following:
string = "ab1cd1ef"
string = string.replace("1","")
print string
# result: "abcdef"
Put it in a loop:
a = "a!b@c#d$"
b = "!@#$"
for char in b:
a = a.replace(char,"")
print a
# result: "abcd"
回答 2
>>> line = "abc#@!?efg12;:?"
>>> ''.join( c for c in line if c not in '?:!/;' )
'abc#@efg12'
>>> line = "abc#@!?efg12;:?"
>>> ''.join( c for c in line if c not in '?:!/;' )
'abc#@efg12'
回答 3
re.sub
从Python 3.5开始具有正则表达式
re.sub('\ |\?|\.|\!|\/|\;|\:', '', line)
例
>>> import re
>>> line = 'Q: Do I write ;/.??? No!!!'
>>> re.sub('\ |\?|\.|\!|\/|\;|\:', '', line)
'QDoIwriteNo'
说明
在正则表达式(regex)中,|
它是逻辑OR,并\
转义可能是实际regex命令的空格和特殊字符。而sub
代表替换,在这种情况下为空字符串''
。
Easy peasy with re.sub
regular expression as of Python 3.5
re.sub('\ |\?|\.|\!|\/|\;|\:', '', line)
Example
>>> import re
>>> line = 'Q: Do I write ;/.??? No!!!'
>>> re.sub('\ |\?|\.|\!|\/|\;|\:', '', line)
'QDoIwriteNo'
Explanation
In regular expressions (regex), |
is a logical OR and \
escapes spaces and special characters that might be actual regex commands. Whereas sub
stands for substitution, in this case with the empty string ''
.
回答 4
对于只允许在字符串中使用某些字符的相反要求,可以将正则表达式与集合补码运算符配合使用[^ABCabc]
。例如,要删除除ASCII字母,数字和连字符以外的所有内容,请执行以下操作:
>>> import string
>>> import re
>>>
>>> phrase = ' There were "nine" (9) chick-peas in my pocket!!! '
>>> allow = string.letters + string.digits + '-'
>>> re.sub('[^%s]' % allow, '', phrase)
'Therewerenine9chick-peasinmypocket'
从python正则表达式文档中:
可以通过补充集合来匹配不在范围内的字符。如果集合的第一个字符是'^'
,则所有不在集合中的字符都将被匹配。例如,[^5]
将匹配除“ 5”以外的任何字符,并将匹配除以外的[^^]
任何字符
'^'
。^
如果不是集合中的第一个字符,则没有特殊含义。
For the inverse requirement of only allowing certain characters in a string, you can use regular expressions with a set complement operator [^ABCabc]
. For example, to remove everything except ascii letters, digits, and the hyphen:
>>> import string
>>> import re
>>>
>>> phrase = ' There were "nine" (9) chick-peas in my pocket!!! '
>>> allow = string.letters + string.digits + '-'
>>> re.sub('[^%s]' % allow, '', phrase)
'Therewerenine9chick-peasinmypocket'
From the python regular expression documentation:
Characters that are not within a range can be matched by complementing
the set. If the first character of the set is '^'
, all the characters
that are not in the set will be matched. For example, [^5]
will match
any character except ‘5’, and [^^]
will match any character except
'^'
. ^
has no special meaning if it’s not the first character in the
set.
回答 5
询问者几乎拥有了它。像Python中的大多数事物一样,答案比您想象的要简单。
>>> line = "H E?.LL!/;O:: "
>>> for char in ' ?.!/;:':
... line = line.replace(char,'')
...
>>> print line
HELLO
您不必执行嵌套的if / for循环操作,但是您需要单独检查每个字符。
The asker almost had it. Like most things in Python, the answer is simpler than you think.
>>> line = "H E?.LL!/;O:: "
>>> for char in ' ?.!/;:':
... line = line.replace(char,'')
...
>>> print line
HELLO
You don’t have to do the nested if/for loop thing, but you DO need to check each character individually.
回答 6
line = line.translate(None, " ?.!/;:")
line = line.translate(None, " ?.!/;:")
回答 7
>>> s = 'a1b2c3'
>>> ''.join(c for c in s if c not in '123')
'abc'
>>> s = 'a1b2c3'
>>> ''.join(c for c in s if c not in '123')
'abc'
回答 8
字符串在Python中是不可变的。replace
替换后,该方法返回一个新字符串。尝试:
for char in line:
if char in " ?.!/;:":
line = line.replace(char,'')
Strings are immutable in Python. The replace
method returns a new string after the replacement. Try:
for char in line:
if char in " ?.!/;:":
line = line.replace(char,'')
回答 9
令我惊讶的是,还没有人建议使用内置过滤器功能。
import operator
import string # only for the example you could use a custom string
s = "1212edjaq"
假设我们要过滤掉所有不是数字的内容。使用过滤器内置方法“ …等效于生成器表达式(如果函数(item),则为可迭代的项目项)” [ Python 3内置:过滤器 ]
sList = list(s)
intsList = list(string.digits)
obj = filter(lambda x: operator.contains(intsList, x), sList)))
在Python 3中返回
>> <filter object @ hex>
要获得打印的字符串,
nums = "".join(list(obj))
print(nums)
>> "1212"
我不确定过滤器在效率方面如何排名,但是知道如何在进行列表理解等时使用过滤器是一件好事。
更新
从逻辑上讲,由于过滤器可以工作,因此您还可以使用列表理解功能,并且据我所读,由于lambda是编程功能领域的华尔街对冲基金经理,因此应该更有效。另一个优点是它是一种单线,不需要任何进口。例如,使用上面定义的相同字符串“ s”,
num = "".join([i for i in s if i.isdigit()])
而已。返回值将是原始字符串中所有数字字符的字符串。
如果您有可接受/不可接受字符的特定列表,则只需调整列表理解的’if’部分。
target_chars = "".join([i for i in s if i in some_list])
或者,
target_chars = "".join([i for i in s if i not in some_list])
I was surprised that no one had yet recommended using the builtin filter function.
import operator
import string # only for the example you could use a custom string
s = "1212edjaq"
Say we want to filter out everything that isn’t a number. Using the filter builtin method “…is equivalent to the generator expression (item for item in iterable if function(item))” [Python 3 Builtins: Filter]
sList = list(s)
intsList = list(string.digits)
obj = filter(lambda x: operator.contains(intsList, x), sList)))
In Python 3 this returns
>> <filter object @ hex>
To get a printed string,
nums = "".join(list(obj))
print(nums)
>> "1212"
I am not sure how filter ranks in terms of efficiency but it is a good thing to know how to use when doing list comprehensions and such.
UPDATE
Logically, since filter works you could also use list comprehension and from what I have read it is supposed to be more efficient because lambdas are the wall street hedge fund managers of the programming function world. Another plus is that it is a one-liner that doesnt require any imports. For example, using the same string ‘s’ defined above,
num = "".join([i for i in s if i.isdigit()])
That’s it. The return will be a string of all the characters that are digits in the original string.
If you have a specific list of acceptable/unacceptable characters you need only adjust the ‘if’ part of the list comprehension.
target_chars = "".join([i for i in s if i in some_list])
or alternatively,
target_chars = "".join([i for i in s if i not in some_list])
回答 10
使用filter
,您只需要一行
line = filter(lambda char: char not in " ?.!/;:", line)
这会将字符串视为可迭代的,并检查每个字符是否lambda
返回True
:
>>> help(filter)
Help on built-in function filter in module __builtin__:
filter(...)
filter(function or None, sequence) -> list, tuple, or string
Return those items of sequence for which function(item) is true. If
function is None, return the items that are true. If sequence is a tuple
or string, return the same type, else return a list.
Using filter
, you’d just need one line
line = filter(lambda char: char not in " ?.!/;:", line)
This treats the string as an iterable and checks every character if the lambda
returns True
:
>>> help(filter)
Help on built-in function filter in module __builtin__:
filter(...)
filter(function or None, sequence) -> list, tuple, or string
Return those items of sequence for which function(item) is true. If
function is None, return the items that are true. If sequence is a tuple
or string, return the same type, else return a list.
回答 11
这是完成此任务的一些可能方法:
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"))
PS:示例中使用的是元音…,而不是“?。!/ ;:”,是的,“ 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
因此,对于这种特定输入,似乎try4是最快的尝试。
Here’s some possible ways to achieve this task:
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"))
PS: Instead using ” ?.!/;:” the examples use the vowels… and yeah, “murcielago” is the Spanish word to say bat… funny word as it contains all the vowels :)
PS2: If you’re interested on performance you could measure these attempts with a simple code like:
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))
In my box you’d get:
attempt1 2.2334518376057244
attempt2 1.8806643818474513
attempt3 7.214925774955572
attempt4 1.7271184513757465
So it seems attempt4 is the fastest one for this particular input.
回答 12
这是我的Python 2/3兼容版本。由于翻译API已更改。
def remove(str_, chars):
"""Removes each char in `chars` from `str_`.
Args:
str_: String to remove characters from
chars: String of to-be removed characters
Returns:
A copy of str_ with `chars` removed
Example:
remove("What?!?: darn;", " ?.!:;") => 'Whatdarn'
"""
try:
# Python2.x
return str_.translate(None, chars)
except TypeError:
# Python 3.x
table = {ord(char): None for char in chars}
return str_.translate(table)
Here’s my Python 2/3 compatible version. Since the translate api has changed.
def remove(str_, chars):
"""Removes each char in `chars` from `str_`.
Args:
str_: String to remove characters from
chars: String of to-be removed characters
Returns:
A copy of str_ with `chars` removed
Example:
remove("What?!?: darn;", " ?.!:;") => 'Whatdarn'
"""
try:
# Python2.x
return str_.translate(None, chars)
except TypeError:
# Python 3.x
table = {ord(char): None for char in chars}
return str_.translate(table)
回答 13
#!/usr/bin/python
import re
strs = "how^ much for{} the maple syrup? $20.99? That's[] ricidulous!!!"
print strs
nstr = re.sub(r'[?|$|.|!|a|b]',r' ',strs)#i have taken special character to remove but any #character can be added here
print nstr
nestr = re.sub(r'[^a-zA-Z0-9 ]',r'',nstr)#for removing special character
print nestr
#!/usr/bin/python
import re
strs = "how^ much for{} the maple syrup? $20.99? That's[] ricidulous!!!"
print strs
nstr = re.sub(r'[?|$|.|!|a|b]',r' ',strs)#i have taken special character to remove but any #character can be added here
print nstr
nestr = re.sub(r'[^a-zA-Z0-9 ]',r'',nstr)#for removing special character
print nestr
回答 14
这个怎么样:
def text_cleanup(text):
new = ""
for i in text:
if i not in " ?.!/;:":
new += i
return new
How about this:
def text_cleanup(text):
new = ""
for i in text:
if i not in " ?.!/;:":
new += i
return new
回答 15
您还可以使用一个函数,以使用列表替换其他种类的正则表达式或其他模式。这样,您就可以混合使用正则表达式,字符类和真正的基本文本模式。当您需要替换许多HTML元素时,它非常有用。
*注意:适用于Python 3.x
import re # Regular expression library
def string_cleanup(x, notwanted):
for item in notwanted:
x = re.sub(item, '', x)
return x
line = "<title>My example: <strong>A text %very% $clean!!</strong></title>"
print("Uncleaned: ", line)
# Get rid of html elements
html_elements = ["<title>", "</title>", "<strong>", "</strong>"]
line = string_cleanup(line, html_elements)
print("1st clean: ", line)
# Get rid of special characters
special_chars = ["[!@#$]", "%"]
line = string_cleanup(line, special_chars)
print("2nd clean: ", line)
在函数string_cleanup中,它将字符串x和不需要的列表作为参数。对于该元素或模式列表中的每个项目,如果需要替代,它将完成。
输出:
Uncleaned: <title>My example: <strong>A text %very% $clean!!</strong></title>
1st clean: My example: A text %very% $clean!!
2nd clean: My example: A text very clean
You can also use a function in order to substitute different kind of regular expression or other pattern with the use of a list. With that, you can mixed regular expression, character class, and really basic text pattern. It’s really useful when you need to substitute a lot of elements like HTML ones.
*NB: works with Python 3.x
import re # Regular expression library
def string_cleanup(x, notwanted):
for item in notwanted:
x = re.sub(item, '', x)
return x
line = "<title>My example: <strong>A text %very% $clean!!</strong></title>"
print("Uncleaned: ", line)
# Get rid of html elements
html_elements = ["<title>", "</title>", "<strong>", "</strong>"]
line = string_cleanup(line, html_elements)
print("1st clean: ", line)
# Get rid of special characters
special_chars = ["[!@#$]", "%"]
line = string_cleanup(line, special_chars)
print("2nd clean: ", line)
In the function string_cleanup, it takes your string x and your list notwanted as arguments. For each item in that list of elements or pattern, if a substitute is needed it will be done.
The output:
Uncleaned: <title>My example: <strong>A text %very% $clean!!</strong></title>
1st clean: My example: A text %very% $clean!!
2nd clean: My example: A text very clean
回答 16
我使用的方法可能无法有效地工作,但是它非常简单。我可以使用切片和格式化功能一次删除不同位置的多个字符。这是一个例子:
words = "things"
removed = "%s%s" % (words[:3], words[-1:])
这将导致“删除”中带有“ this”一词。
格式化对于在打印字符串中途打印变量非常有用。它可以使用%插入任何数据类型,后跟变量的数据类型。所有数据类型都可以使用%s,而浮点数(也就是小数)和整数可以使用%d。
切片可用于对字符串的复杂控制。当我输入words [:3]时,它允许我从字符串的开头选择所有字符(冒号在数字之前,这意味着“从开头到”)到第四个字符(包括第四个字符)字符)。之所以3等于第4位是因为Python从0开始。然后,当我将word [-1:]放到最后时,倒数第二个字符(冒号在数字后面)。放置-1将使Python从最后符开始计数,而不是从第一个字符开始计数。同样,Python将从0开始。因此,单词[-1:]基本上表示’从倒数第二个字符到字符串的末尾。
因此,通过剪掉我要删除的字符之前的字符,之后要剪掉的字符并将它们夹在中间,我可以删除不需要的字符。想起来像香肠。中间很脏,所以我想摆脱它。我只剪掉我想要的两端,然后将它们放在一起,中间没有多余的部分。
如果要删除多个连续的字符,只需在[](切片部分)中移动数字即可。或者,如果我想从不同位置删除多个字符,则可以一次将多个切片夹在一起。
例子:
words = "control"
removed = "%s%s" % (words[:2], words[-2:])
已移除等于“酷”。
words = "impacts"
removed = "%s%s%s" % (words[1], words[3:5], words[-1])
已移除等于“ macs”。
在这种情况下,[3:5]表示位置 3到位置处的字符位置的 5的字符(不包括最终位置的字符)。
请记住,Python从0开始计数,因此您也需要这样做。
My method I’d use probably wouldn’t work as efficiently, but it is massively simple. I can remove multiple characters at different positions all at once, using slicing and formatting. Here’s an example:
words = "things"
removed = "%s%s" % (words[:3], words[-1:])
This will result in ‘removed’ holding the word ‘this’.
Formatting can be very helpful for printing variables midway through a print string. It can insert any data type using a % followed by the variable’s data type; all data types can use %s, and floats (aka decimals) and integers can use %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.
So, by cutting off the characters before the character I want to remove and the characters after and sandwiching them together, I can remove the unwanted character. Think of it like a sausage. In the middle it’s dirty, so I want to get rid of it. I simply cut off the two ends I want then put them together without the unwanted part in the middle.
If I want to remove multiple consecutive characters, I simply shift the numbers around in the [] (slicing part). Or if I want to remove multiple characters from different positions, I can simply sandwich together multiple slices at once.
Examples:
words = "control"
removed = "%s%s" % (words[:2], words[-2:])
removed equals ‘cool’.
words = "impacts"
removed = "%s%s%s" % (words[1], words[3:5], words[-1])
removed equals ‘macs’.
In this case, [3:5] means character at position 3 through character at position 5 (excluding the character at the final position).
Remember, Python starts counting at 0, so you will need to as well.
回答 17
试试这个:
def rm_char(original_str, need2rm):
''' Remove charecters in "need2rm" from "original_str" '''
return original_str.translate(str.maketrans('','',need2rm))
此方法在python 3.5.2中很好用
Try this one:
def rm_char(original_str, need2rm):
''' Remove charecters in "need2rm" from "original_str" '''
return original_str.translate(str.maketrans('','',need2rm))
This method works well in python 3.5.2
回答 18
您可以使用re模块的正则表达式替换。使用^表达式可让您从字符串中准确选择所需的内容。
import re
text = "This is absurd!"
text = re.sub("[^a-zA-Z]","",text) # Keeps only Alphabets
print(text)
输出为“ Thisisabsurd”。仅出现在^符号后指定的内容。
You could use the re module’s regular expression replacement. Using the ^ expression allows you to pick exactly what you want from your string.
import re
text = "This is absurd!"
text = re.sub("[^a-zA-Z]","",text) # Keeps only Alphabets
print(text)
Output to this would be “Thisisabsurd”. Only things specified after the ^ symbol will appear.
回答 19
字符串方法replace
不会修改原始字符串。它保留原始文件,并返回修改后的副本。
您想要的是这样的: line = line.replace(char,'')
def replace_all(line, )for char in line:
if char in " ?.!/;:":
line = line.replace(char,'')
return line
但是,每次删除一个字符时都创建一个新字符串是非常低效的。我建议改为以下内容:
def replace_all(line, baddies, *):
"""
The following is documentation on how to use the class,
without reference to the implementation details:
For implementation notes, please see comments begining with `#`
in the source file.
[*crickets chirp*]
"""
is_bad = lambda ch, baddies=baddies: return ch in baddies
filter_baddies = lambda ch, *, is_bad=is_bad: "" if is_bad(ch) else ch
mahp = replace_all.map(filter_baddies, line)
return replace_all.join('', join(mahp))
# -------------------------------------------------
# WHY `baddies=baddies`?!?
# `is_bad=is_bad`
# -------------------------------------------------
# Default arguments to a lambda function are evaluated
# at the same time as when a lambda function is
# **defined**.
#
# global variables of a lambda function
# are evaluated when the lambda function is
# **called**
#
# The following prints "as yellow as snow"
#
# fleece_color = "white"
# little_lamb = lambda end: return "as " + fleece_color + end
#
# # sometime later...
#
# fleece_color = "yellow"
# print(little_lamb(" as snow"))
# --------------------------------------------------
replace_all.map = map
replace_all.join = str.join
The string method replace
does not modify the original string. It leaves the original alone and returns a modified copy.
What you want is something like: line = line.replace(char,'')
def replace_all(line, )for char in line:
if char in " ?.!/;:":
line = line.replace(char,'')
return line
However, creating a new string each and every time that a character is removed is very inefficient. I recommend the following instead:
def replace_all(line, baddies, *):
"""
The following is documentation on how to use the class,
without reference to the implementation details:
For implementation notes, please see comments begining with `#`
in the source file.
[*crickets chirp*]
"""
is_bad = lambda ch, baddies=baddies: return ch in baddies
filter_baddies = lambda ch, *, is_bad=is_bad: "" if is_bad(ch) else ch
mahp = replace_all.map(filter_baddies, line)
return replace_all.join('', join(mahp))
# -------------------------------------------------
# WHY `baddies=baddies`?!?
# `is_bad=is_bad`
# -------------------------------------------------
# Default arguments to a lambda function are evaluated
# at the same time as when a lambda function is
# **defined**.
#
# global variables of a lambda function
# are evaluated when the lambda function is
# **called**
#
# The following prints "as yellow as snow"
#
# fleece_color = "white"
# little_lamb = lambda end: return "as " + fleece_color + end
#
# # sometime later...
#
# fleece_color = "yellow"
# print(little_lamb(" as snow"))
# --------------------------------------------------
replace_all.map = map
replace_all.join = str.join
回答 20
下面的一个..不使用正则表达式的概念..
ipstring ="text with symbols!@#$^&*( ends here"
opstring=''
for i in ipstring:
if i.isalnum()==1 or i==' ':
opstring+=i
pass
print opstring
Below one.. with out using regular expression concept..
ipstring ="text with symbols!@#$^&*( ends here"
opstring=''
for i in ipstring:
if i.isalnum()==1 or i==' ':
opstring+=i
pass
print opstring
回答 21
在Python 3.5中
例如,
os.rename(file_name, file_name.translate({ord(c): None for c in '0123456789'}))
从字符串中删除所有数字
In Python 3.5
e.g.,
os.rename(file_name, file_name.translate({ord(c): None for c in '0123456789'}))
To remove all the number from the string
回答 22
你可以使用设置
charlist = list(set(string.digits+string.ascii_uppercase) - set('10IO'))
return ''.join([random.SystemRandom().choice(charlist) for _ in range(passlen)])
you can use set
charlist = list(set(string.digits+string.ascii_uppercase) - set('10IO'))
return ''.join([random.SystemRandom().choice(charlist) for _ in range(passlen)])
回答 23
递归拆分: s = string; chars =要删除的字符
def strip(s,chars):
if len(s)==1:
return "" if s in chars else s
return strip(s[0:int(len(s)/2)],chars) + strip(s[int(len(s)/2):len(s)],chars)
例:
print(strip("Hello!","lo")) #He!
Recursive split: s=string ; chars=chars to remove
def strip(s,chars):
if len(s)==1:
return "" if s in chars else s
return strip(s[0:int(len(s)/2)],chars) + strip(s[int(len(s)/2):len(s)],chars)
example:
print(strip("Hello!","lo")) #He!
回答 24
#为目录中的每个文件重命名文件名
file_list = os.listdir (r"D:\Dev\Python")
for file_name in file_list:
os.rename(file_name, re.sub(r'\d+','',file_name))
# for each file on a directory, rename filename
file_list = os.listdir (r"D:\Dev\Python")
for file_name in file_list:
os.rename(file_name, re.sub(r'\d+','',file_name))
回答 25
即使是以下方法也可以
line = "a,b,c,d,e"
alpha = list(line)
while ',' in alpha:
alpha.remove(',')
finalString = ''.join(alpha)
print(finalString)
输出: abcde
Even the below approach works
line = "a,b,c,d,e"
alpha = list(line)
while ',' in alpha:
alpha.remove(',')
finalString = ''.join(alpha)
print(finalString)
output: abcde
回答 26
>>> # Character stripping
>>> a = '?abcd1234!!'
>>> t.lstrip('?')
'abcd1234!!'
>>> t.strip('?!')
'abcd1234'
>>> # Character stripping
>>> a = '?abcd1234!!'
>>> t.lstrip('?')
'abcd1234!!'
>>> t.strip('?!')
'abcd1234'
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。