我需要从字符串中隔离所有整数,列出它们,并打印它们的总和和整数数。我的问题是我当前的代码会将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]
答案 0 :(得分:1)
这是一个单行:
>>> s = '34 ch33se 34e8 3.4'
>>> map(int, filter(None, ''.join(map(lambda c: (c.isdigit() and c or ' '), s)).split(' ')))
[34, 33, 34, 8, 3, 4]
答案 1 :(得分:0)
def get_integers(string):
current_integer = ""
for i in string:
if i.isdigit():
current_integer += str(i)
else:
if current_integer:
yield int(current_integer)
current_integer = ""
if current_integer:
yield int(current_integer)
# Prints [34, 33, 34, 8, 3, 4]
print([i for i in get_integers('34 ch33se 34e8 3.4')])
你应该能够sum
或其结果。
答案 2 :(得分:0)
为什么不创建一个按字符搜索字符串并抓取数字的函数?像这样:
def find_numbers(string):
numbers = []
usable = True #mark unusable if its inside a word
latest_number = ''
for char in string:
if char in '1234567890' and usable:
latest_number += char
continue
elif latest_number:
numbers.append(int(latest_number))
latest_number = ''
if char in ' ,':
usable = True
else:
usable = False
if latest_number: numbers.append(int(latest_number))
return numbers
使用此功能,您可以轻松获取数字:
>>> a = 'hello 123 wh3at? 2'
>>> find_numbers(a)
[123, 2]
编辑:
如果要包含单词内的整数:
def find_numbers(string):
numbers = []
latest_number = ''
for char in string:
if char in '1234567890':
latest_number += char
elif latest_number:
numbers.append(int(latest_number))
latest_number = ''
if latest_number: numbers.append(int(latest_number))
return numbers
答案 3 :(得分:0)
使用itertools.groupby
的选项:
>>> from itertools import groupby
>>> s = '34 ch33se 34e8 3.4'
>>>
>>> filter(str.isdigit, (''.join(g) for k,g in groupby(s, key=str.isdigit)))
<filter object at 0x1006ebc50>
>>> list(_)
['34', '33', '34', '8', '3', '4']
这明显快于接受的答案:
>>> from timeit import timeit
>>>
>>> setup = """
... from itertools import groupby
... s = '34 ch33se 34e8 3.4'
... """
>>>
>>> timeit("map(int, filter(None, ''.join(map(lambda c: (c.isdigit() and c or ' '), s)).split(' ')))", setup)
6.1007173559919465
>>> timeit("filter(str.isdigit, (''.join(g) for k,g in groupby(s, key=str.isdigit)))", setup)
1.5044781469914597