Python 从字符串中删除数字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12851791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:00:35  来源:igfitidea点击:

Removing numbers from string

pythonstring

提问by user1739954

How can I remove digits from a string?

如何从字符串中删除数字?

采纳答案by RocketDonkey

Would this work for your situation?

这对你的情况有用吗?

>>> s = '12abcd405'
>>> result = ''.join([i for i in s if not i.isdigit()])
>>> result
'abcd'

This makes use of a list comprehension, and what is happening here is similar to this structure:

这使用了列表推导式,这里发生的事情类似于这个结构:

no_digits = []
# Iterate through the string, adding non-numbers to the no_digits list
for i in s:
    if not i.isdigit():
        no_digits.append(i)

# Now join all elements of the list with '', 
# which puts all of the characters together.
result = ''.join(no_digits)

As @AshwiniChaudhary and @KirkStrauser point out, you actually do not need to use the brackets in the one-liner, making the piece inside the parentheses a generator expression (more efficient than a list comprehension). Even if this doesn't fit the requirements for your assignment, it is something you should read about eventually :) :

正如@AshwiniChaudhary 和@KirkStrauser 指出的那样,您实际上不需要在单行中使用括号,从而使括号内的部分成为生成器表达式(比列表理解更有效)。即使这不符合您的任务要求,您最终也应该阅读它:):

>>> s = '12abcd405'
>>> result = ''.join(i for i in s if not i.isdigit())
>>> result
'abcd'

回答by Sean Johnson

I'd love to use regex to accomplish this, but since you can only use lists, loops, functions, etc..

我很想使用正则表达式来完成此操作,但由于您只能使用列表、循环、函数等。

here's what I came up with:

这是我想出的:

stringWithNumbers="I have 10 bananas for my 5 monkeys!"
stringWithoutNumbers=''.join(c if c not in map(str,range(0,10)) else "" for c in stringWithNumbers)
print(stringWithoutNumbers) #I have  bananas for my  monkeys!

回答by Baahubali

If i understand your question right, one way to do is break down the string in chars and then check each char in that string using a loop whether it's a string or a number and then if string save it in a variable and then once the loop is finished, display that to the user

如果我理解你的问题是正确的,一种方法是将字符串分解为字符,然后使用循环检查该字符串中的每个字符是字符串还是数字,然后如果字符串将其保存在变量中,然后进行一次循环完成,显示给用户

回答by Pavel Paulau

What about this:

那这个呢:

out_string = filter(lambda c: not c.isdigit(), in_string)

回答by iddqd

Say st is your unformatted string, then run

说 st 是你的未格式化字符串,然后运行

st_nodigits=''.join(i for i in st if i.isalpha())

as mentioned above. But my guess that you need something very simple so say sis your string and st_resis a string without digits, then here is your code

正如刚才提到的。但我猜你需要一些非常简单的东西,所以说s是你的字符串,st_res是一个没有数字的字符串,那么这里是你的代码

l = ['0','1','2','3','4','5','6','7','8','9']
st_res=""
for ch in s:
 if ch not in l:
  st_res+=ch

回答by Alain Nisam

Not sure if your teacher allows you to use filters but...

不确定您的老师是否允许您使用过滤器,但是...

filter(lambda x: x.isalpha(), "a1a2a3s3d4f5fg6h")

returns-

返回-

'aaasdffgh'

Much more efficient than looping...

比循环更有效...

Example:

例子:

for i in range(10):
  a.replace(str(i),'')

回答by inspectorG4dget

Just a few (others have suggested some of these)

只是一些(其他人已经建议了其中的一些)

Method 1:

方法一:

''.join(i for i in myStr if not i.isdigit())

Method 2:

方法二:

def removeDigits(s):
    answer = []
    for char in s:
        if not char.isdigit():
            answer.append(char)
    return ''.join(char)

Method 3:

方法三:

''.join(filter(lambda x: not x.isdigit(), mystr))

Method 4:

方法四:

nums = set(map(int, range(10)))
''.join(i for i in mystr if i not in nums)

Method 5:

方法五:

''.join(i for i in mystr if ord(i) not in range(48, 58))

回答by Jon Clements

And, just to throw it in the mix, is the oft-forgotten str.translatewhich will work a lot faster than looping/regular expressions:

而且,只是把它放在混合中,是经常被遗忘的str.translate,它比循环/正则表达式的工作速度要快得多:

For Python 2:

对于 Python 2:

from string import digits

s = 'abc123def456ghi789zero0'
res = s.translate(None, digits)
# 'abcdefghizero'

For Python 3:

对于 Python 3:

from string import digits

s = 'abc123def456ghi789zero0'
remove_digits = str.maketrans('', '', digits)
res = s.translate(remove_digits)
# 'abcdefghizero'