我需要从字符串中分离出所有整数,列出它们,并打印它们的总和和整数的#。我的问题是我当前的代码会将例如 456 拆分为 4、5 和 6,并将它们视为单独的整数。不幸的是,正则表达式不是一种选择。
到目前为止我所拥有的:
def tally(text):
s = ','.join(x for x in text if x.isdigit())
numbers = [int(x) for x in s.split(",")]
num=len(numbers)
t=sum(numbers)
print ('There are', num, 'integers in the input summing up to', t)
.
What i need: input:'34 ch33se 34e8 3.4'
output: [34 33 34 8 3 4 ]
im getting now is [3 4 3 3 8 3 4]