Python 从数字和字母列表中删除字母
在我试图编写的函数中,用户输入一组数字,例如“648392”。 我将这个字符串转换成如下列表:['6','4','8','3','9','2'] 我希望能够用这些数字求和,所以我把列表中的数字转换成整数而不是字符串。这一切都很好,但是我也希望用户能够输入字母,然后我会将它们从列表中删除-这就是我被卡住的地方。例如,用户输入“6483A2”Python 从数字和字母列表中删除字母,python,Python,在我试图编写的函数中,用户输入一组数字,例如“648392”。 我将这个字符串转换成如下列表:['6','4','8','3','9','2'] 我希望能够用这些数字求和,所以我把列表中的数字转换成整数而不是字符串。这一切都很好,但是我也希望用户能够输入字母,然后我会将它们从列表中删除-这就是我被卡住的地方。例如,用户输入“6483A2” 我无法检查元素是否是带isDigit的数字,因为元素必须首先是整数,并且我无法将列表中的元素转换为整数,因为有些元素是字母。。。我相信有一个简单的解决方案,但
我无法检查元素是否是带isDigit的数字,因为元素必须首先是整数,并且我无法将列表中的元素转换为整数,因为有些元素是字母。。。我相信有一个简单的解决方案,但我在python方面非常糟糕,因此非常感谢任何帮助 您可以使用
str.translate
过滤掉字母:
>>> from string import letters
>>> strs = "6483A2"
>>> strs.translate(None, letters)
'64832'
不需要将字符串转换为列表,您可以迭代字符串本身
使用str.join
,str.isdigit
和列表理解:
>>> ''.join([c for c in strs if c.isdigit()])
'64832'
>>> s = "6483A2"
>>> [int(c) for c in s if c.isdigit()]
[6, 4, 8, 3, 2]
>>> sum(int(c) for c in s if c.isdigit())
23
或者这是您想要的数字总和:
sum(int(c) for c in strs if c.isdigit())
时间比较:
小字符串:
>>> strs = "6483A2"
>>> %timeit sum(int(c) for c in strs.translate(None, letters))
100000 loops, best of 3: 9.19 us per loop
>>> %timeit sum(int(c) for c in strs if c.isdigit())
100000 loops, best of 3: 10.1 us per loop
>>> strs = "6483A2"*1000
>>> %timeit sum(int(c) for c in strs.translate(None, letters))
100 loops, best of 3: 5.47 ms per loop
>>> %timeit sum(int(c) for c in strs if c.isdigit())
100 loops, best of 3: 8.54 ms per loop
>>> strs = "A"*100
>>> %timeit sum(int(c) for c in strs.translate(None, letters))
100000 loops, best of 3: 2.53 us per loop
>>> %timeit sum(int(c) for c in strs if c.isdigit())
10000 loops, best of 3: 24.8 us per loop
>>> strs = "A"*1000
>>> %timeit sum(int(c) for c in strs.translate(None, letters))
100000 loops, best of 3: 7.34 us per loop
>>> %timeit sum(int(c) for c in strs if c.isdigit())
1000 loops, best of 3: 210 us per loop
大字符串:
>>> strs = "6483A2"
>>> %timeit sum(int(c) for c in strs.translate(None, letters))
100000 loops, best of 3: 9.19 us per loop
>>> %timeit sum(int(c) for c in strs if c.isdigit())
100000 loops, best of 3: 10.1 us per loop
>>> strs = "6483A2"*1000
>>> %timeit sum(int(c) for c in strs.translate(None, letters))
100 loops, best of 3: 5.47 ms per loop
>>> %timeit sum(int(c) for c in strs if c.isdigit())
100 loops, best of 3: 8.54 ms per loop
>>> strs = "A"*100
>>> %timeit sum(int(c) for c in strs.translate(None, letters))
100000 loops, best of 3: 2.53 us per loop
>>> %timeit sum(int(c) for c in strs if c.isdigit())
10000 loops, best of 3: 24.8 us per loop
>>> strs = "A"*1000
>>> %timeit sum(int(c) for c in strs.translate(None, letters))
100000 loops, best of 3: 7.34 us per loop
>>> %timeit sum(int(c) for c in strs if c.isdigit())
1000 loops, best of 3: 210 us per loop
最坏情况,所有字母:
>>> strs = "6483A2"
>>> %timeit sum(int(c) for c in strs.translate(None, letters))
100000 loops, best of 3: 9.19 us per loop
>>> %timeit sum(int(c) for c in strs if c.isdigit())
100000 loops, best of 3: 10.1 us per loop
>>> strs = "6483A2"*1000
>>> %timeit sum(int(c) for c in strs.translate(None, letters))
100 loops, best of 3: 5.47 ms per loop
>>> %timeit sum(int(c) for c in strs if c.isdigit())
100 loops, best of 3: 8.54 ms per loop
>>> strs = "A"*100
>>> %timeit sum(int(c) for c in strs.translate(None, letters))
100000 loops, best of 3: 2.53 us per loop
>>> %timeit sum(int(c) for c in strs if c.isdigit())
10000 loops, best of 3: 24.8 us per loop
>>> strs = "A"*1000
>>> %timeit sum(int(c) for c in strs.translate(None, letters))
100000 loops, best of 3: 7.34 us per loop
>>> %timeit sum(int(c) for c in strs if c.isdigit())
1000 loops, best of 3: 210 us per loop
您可以使用
filter
函数或理解从任何iterable(包括字符串)中过滤出内容。例如,以下任一项:
digits = filter(str.isdigit, input_string)
digits = (character for character in input_string if character.isdigit())
…将为您提供一个充满数字的iterable。如果要将每一个转换为一个数字,以下任一项都可以:
numbers = map(int, filter(str.isdigit, input_string))
numbers = (int(character) for character in input_string if character.isdigit())
因此,要获得所有数字的总和,跳过字母,只需将其中一个传递给sum
函数:
total = sum(map(int, filter(str.isdigit, input_string)))
total = sum(int(character) for character in input_string if character.isdigit())
从你的最后一段: 我无法检查元素是否是带isDigit的数字,因为元素显然必须首先是整数,并且我无法将列表中的元素转换为整数 首先,它是
isdigit
,而不是isdigit
。其次,isdigit
是一个字符串方法,而不是整数,因此您认为不能在字符串上调用它是错误的。事实上,在将字符串转换为整数之前,必须对其进行调用
但这确实带来了另一种选择。在Python中,它通常是。我们可以试着将每个字母转换为int,然后处理可能出现的故障,而不是计算是否可以将每个字母转换为int。例如:
def get_numbers(input_string):
for character in input_string:
try:
yield int(character)
except TypeError:
pass
现在,它只是:
total = sum(get_numbers(input_string))
您可以理解以下内容:
>>> ''.join([c for c in strs if c.isdigit()])
'64832'
>>> s = "6483A2"
>>> [int(c) for c in s if c.isdigit()]
[6, 4, 8, 3, 2]
>>> sum(int(c) for c in s if c.isdigit())
23
如果您想直接从混合字符串转到仅包含整数的列表,那么这种方法很好,这大概是您的目标。您可以使用a并将其放入和中
>>> import string
>>> s
'6483A2'
>>> sum(int(x) for x in list(s) if x in string.digits)
23
如果没有其他模块要导入,请使用isdigit
:
sum(int(x) for x in list(s) if x.isdigit())
当然,你可以用各种方式过滤掉字母,但让解释器决定什么可以被解释为数字,什么不能被解释,这可能更像是一种恶作剧。因此,尽管它不是一条直线,但您可能更喜欢这种方法:
aninput = "648392A0&sle4"
def discard_non_ints(itbl, rdx=10):
for d in itbl:
try:
yield(int(d, rdx))
except ValueError:
pass
sum(discard_non_ints(aninput))
36
这种方法特别好的地方在于,它可以灵活地包含非十进制数字。要对所有十六进制数字求和吗
sum(discard_non_ints('deadbeforenoon', 16))
104
只要贡献一点,如果你想要累计金额,你可以这样做:
x = "6483A2"
sum(map(int, filter(str.isdigit, x)))
>>>23
如果您仅出于其他目的或其他类型的sum
需要整数列表,则只需将其保留在map
中即可:
map(int, filter(str.isdigit, x))
>>>[6, 4, 8, 3, 2]
注意:关于string.letters方法<代码>字母
与语言环境
相关,因此:
import locale, string
locale.setlocale(locale.LC_ALL, 'es_ES') # or 'esp_esp' if you're on Windows
string.letters
>>> "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzŠŚŽšśžźŞµşŔÁÂĂÄĹĆÇČÉĘËĚÍÎĎĐŃŇÓÔŐÖŘŮÚŰÜÝŢßŕáâăäĺćçčéęëěíîďđńňóôőöřůúűüýţ˙"
尽管我建议将regex
用于上述情况:)
很高兴合作:D我本来想给出一个正则表达式的答案,但这太棒了!这实际上让我想起了所有那些我做了一些事情(现在我知道这很愚蠢)的时候,比如:
abc='abcdefghijklmnopqrst…'
LOL:dw为什么要在末尾添加join
?最终,她想把数字转换成整数序列;将它们打包回一个字符串并不能让你更接近这个目标。@abarnert也添加了sum
版本,只是想用str.join
@AshwiniChaudhary显示一个等价的str.translate
版本,如果引入非ascii字符呢?string
模块是否支持i18n?对于py2.x上的大字符串,请使用itertools.imap
和itertools.ifilter
。确实,谢谢!)我只想给出尽可能少的代码:DIMHO,map(int,filter(str.isdigit,text))
是最干净、最明显的方法。。。只包含正匹配的元素,而不是删除非正的…@JonClements:有些人要么不获取要么不喜欢map
和filter
任何非琐碎的、甚至是未绑定的方法,比如str.isdigit
。我同意你的观点,在这种情况下,它对我来说是最具可读性的…但我也想给出另一种选择,所以我加入了genexpr版本,让读者进行比较和选择。