使用正则表达式查找表单的字符串:
<count> <longword>
e.g. 101 Dalmations.
更具体地说,这场比赛非常符合以下条件:
(word1)
是由自然数组成的计数
(一个或多个数字的系列,带有前导和尾随空格)(word1, word2)
对(如果存在)
如果没有找到这样的字符串,则为无。例如:
parse_counted_words('5 watermelons, 13 pineapples, and 1 papaya.') should return ('13', 'pineapples')
parse_counted_words('101 dalmations!') should return ('101', 'dalmations')
parse_counted_words('snow white and the 7 dwarves') should return ('7', 'dwarves')
parse_counted_words('goldilocks and the 3 little pigs') should return None, because 'little' has less than 7 characters
parse_counted_words('678 1234567 890') should return None, because the word following the count does not consist of alphabetic characters
这是我写的:
def parse_counted_words(s):
m=re.findall(r'\s*\d+\s\w{7,}',s)
if len(m)==0:
return None
elif len(m)>1:
return m[1]
else:
m[0].split
答案 0 :(得分:1)
您可以使用:
s = r'5 watermelons, 13 pineapples, and 1 papaya.'
def parse_counted_words(s):
m=re.findall(r'(?<=\s)\d+\s\w{7,}',s)
if len(m)==0:
return None
else:
return m[-1].split( )
print parse_counted_words(s)