如何使用classify(input_string)
函数返回此内容?
classify("I have 35 apples") returns ([35], ['I', 'have', 'apples'])
我尝试了以下内容:
def classify(input_string):
input_string=[]
split=[]
splits=[]
for val in input_string:
try:
for x in range(len(input_string)):
spilt=input_string[x:input_string]
spilts.append(spilt)
return spilts
except:
return None
答案 0 :(得分:0)
使用split(' ')
拆分文字,然后使用int()
识别数字
def classify(input_string):
words = []
numbers = []
splits = input_string.split(' ')
for element in splits:
try:
val = int(element)
numbers.append(val)
except ValueError:
words.append(element)
return (numbers, words)
classify("I have 35 apples")