从字母数字字符串中提取数字并将它们相加
Extracting number from alphanumeric string and adding them
给定的字符串 str 包含字母数字字符。任务是计算字符串中所有数字的总和。
示例 1:
Input:
str = 1abc23
Output: 24
Explanation: 1 and 23 are numbers in the
a string which is added to get the sum as
24.
示例 2:
Input:
str = geeks4geeks
Output: 4
Explanation: 4 is the only number, so the
the sum is 4.
我把问题分解成更小的部分,首先我只想提取数字。
s = "a12bc3d"
number = ""
for i in range(0, len(s)):
if s[i].isdigit():
n=0
number = number + s[i]
while s[i].isdigit():
n = n+1
if s[i + n].isdigit():
number = number + s[i+n] + " "
else:
break
i = i + n + 1
else:
continue
print(number)
我上面代码的输出是12 23
,但它应该是12 3
,因为for循环是从初始点开始的,所以2出现了两次,我试图移动for循环通过更新 i = i + n + 1
进行转发,但结果并非如此。
如果有人给我指导就太好了,非常感谢任何帮助。
使用itertools.groupby
将字符串分成数字组和not-digits;然后将数字组转换为 int
和 sum
它们:
>>> from itertools import groupby
>>> def sum_numbers(s: str) -> int:
... return sum(int(''.join(g)) for d, g in groupby(s, str.isdigit) if d)
...
>>> sum_numbers("1abc23")
24
>>> sum_numbers("geeks4geeks")
4
你可以使用正则表达式。
import re
s='a12bc3d'
sections = re.split('(\d+)',s)
numeric_sections = [int(x) for x in sections if x.isdigit()]
sum_ = sum(numeric_sections)
print(sum_)
使用正则表达式的稍微简单的方法:
import re
numbers_sum = sum(int(match) for match in re.findall(r'(\d+)', s))
我很欣赏正则表达式和 group-by 的解决方案。我也使用逻辑得到了解决方案。
`s = "4a7312cfh86"
slist = [i for i in s]
nlist = []
for i in range(len(slist)):
if slist[i].isdigit() and (i != (len(slist) - 1)):
if not slist[i + 1].isdigit():
nlist.append(slist[i])
else:
slist[i + 1] = slist[i] + slist[i + 1]
elif slist[i].isdigit() and (i == (len(slist) - 1)):
nlist.append(slist[i])
def addingElement(arr):
if len(arr) == 0:
return 0
return addingElement(arr[1:]) + int(arr[0])
print(addingElement(nlist))
Output - 7402
给定的字符串 str 包含字母数字字符。任务是计算字符串中所有数字的总和。
示例 1:
Input:
str = 1abc23
Output: 24
Explanation: 1 and 23 are numbers in the
a string which is added to get the sum as
24.
示例 2:
Input:
str = geeks4geeks
Output: 4
Explanation: 4 is the only number, so the
the sum is 4.
我把问题分解成更小的部分,首先我只想提取数字。
s = "a12bc3d"
number = ""
for i in range(0, len(s)):
if s[i].isdigit():
n=0
number = number + s[i]
while s[i].isdigit():
n = n+1
if s[i + n].isdigit():
number = number + s[i+n] + " "
else:
break
i = i + n + 1
else:
continue
print(number)
我上面代码的输出是12 23
,但它应该是12 3
,因为for循环是从初始点开始的,所以2出现了两次,我试图移动for循环通过更新 i = i + n + 1
进行转发,但结果并非如此。
如果有人给我指导就太好了,非常感谢任何帮助。
使用itertools.groupby
将字符串分成数字组和not-digits;然后将数字组转换为 int
和 sum
它们:
>>> from itertools import groupby
>>> def sum_numbers(s: str) -> int:
... return sum(int(''.join(g)) for d, g in groupby(s, str.isdigit) if d)
...
>>> sum_numbers("1abc23")
24
>>> sum_numbers("geeks4geeks")
4
你可以使用正则表达式。
import re
s='a12bc3d'
sections = re.split('(\d+)',s)
numeric_sections = [int(x) for x in sections if x.isdigit()]
sum_ = sum(numeric_sections)
print(sum_)
使用正则表达式的稍微简单的方法:
import re
numbers_sum = sum(int(match) for match in re.findall(r'(\d+)', s))
我很欣赏正则表达式和 group-by 的解决方案。我也使用逻辑得到了解决方案。
`s = "4a7312cfh86"
slist = [i for i in s]
nlist = []
for i in range(len(slist)):
if slist[i].isdigit() and (i != (len(slist) - 1)):
if not slist[i + 1].isdigit():
nlist.append(slist[i])
else:
slist[i + 1] = slist[i] + slist[i + 1]
elif slist[i].isdigit() and (i == (len(slist) - 1)):
nlist.append(slist[i])
def addingElement(arr):
if len(arr) == 0:
return 0
return addingElement(arr[1:]) + int(arr[0])
print(addingElement(nlist))
Output - 7402