Python:查找一串浮点数中重复数字序列的长度
Python: Finding length of the repeating sequence of numbers in a string of floats
我正在尝试编写一个函数,它接受一个浮点数列表并确定该列表中是否存在重复的数字序列。如果存在重复序列,则它会计算该序列中的数字。
这些示例是我希望我的函数执行的操作
示例 1:
function = '1.0 8.0 4.0 2.0 1.0 8.0 4.0 2.0 1.0 8.0 4.0 2.0 1.0 8.0'
result = find_repeating_sequence(function)
# repeating sequence = 1.0, 8.0, 4.0, 2.0
# so result should equal 4 in this case
示例 2:
function = '1.0 8.0 4.0 1.0 2.0 1.0 8.0 4.0 1.0 2.0 1.0 8.0'
result = find_repeating_sequence(function)
# repeating sequence = 1.0, 8.0, 4.0, 1.0, 2.0
# so result should equal 5 in this case
示例 3:
function = '1.0 8.0 4.0 2.0 1.0 7.0 6.0 3.0 2.0 5.0 9.0'
result = find_repeating_sequence(function)
# repeating sequence doesn't exist
# so result should equal None in this case
示例 4:
function = '1.0 11.0 1.0 11.0 1.0 11.0 1.0 11.0 1.0 11.0 1.0 11.0 1.0 11.0 1.0 11.0'
result = find_repeating_sequence(function)
# repeating sequence = 1.0, 11.0
# so result should equal 2 in this case
到目前为止我得到的是:
def find_repeating_sequence(function):
regex = re.compile(r'(.+ .+)( )+')
match = regex.search(function)
result = match
if result == None:
print "There was no repeating sequence found!"
else:
print "Repeating sequence found!"
print match.group(1)
result = match.group(1)
return result
这适用于所有示例,因为 match.group(1)
给出了重复序列。但是由于某些原因 len(match.group(1))
不是 return 正确的数字。
如示例 1:
print match.group(1)
给出 1.0 8.0 4.0 2.0
但是 print len(match.group(1))
给出 15
它也不是 return 示例 4 的正确值:
print match.group(1)
给出 1.0 11.0 1.0 11.0 1.0 11.0 1.0 11.0
而 print len(match.group(1))
给出 35
我做错了什么?
更新:
感谢以下答案之一,我使用 len(match.group(1).split())
解决了 print len(match.group(1))
问题
但是由于某些原因如果
function = 1.0 2.0 4.0 8.0 1.0 2.0 4.0 8.0 1.0 2.0 4.0 8.0 1.0 2.0 4.0 8.0
print match.group(1)
给出 1.0 2.0 4.0 8.0 1.0 2.0 4.0 8.0
而不是 1.0 2.0 4.0 8.0
match.group() returns 一个字符串,在本例中是 len() returns 字符串中的字符数。
您可以将字符串拆分 space,然后计算元素的数量
回复更新:
你的表情很贪婪,它试图找到最长的匹配。尝试 (.+? .+?)( )+?
。限定符后的问号使正则表达式变得懒惰:
我正在尝试编写一个函数,它接受一个浮点数列表并确定该列表中是否存在重复的数字序列。如果存在重复序列,则它会计算该序列中的数字。
这些示例是我希望我的函数执行的操作
示例 1:
function = '1.0 8.0 4.0 2.0 1.0 8.0 4.0 2.0 1.0 8.0 4.0 2.0 1.0 8.0'
result = find_repeating_sequence(function)
# repeating sequence = 1.0, 8.0, 4.0, 2.0
# so result should equal 4 in this case
示例 2:
function = '1.0 8.0 4.0 1.0 2.0 1.0 8.0 4.0 1.0 2.0 1.0 8.0'
result = find_repeating_sequence(function)
# repeating sequence = 1.0, 8.0, 4.0, 1.0, 2.0
# so result should equal 5 in this case
示例 3:
function = '1.0 8.0 4.0 2.0 1.0 7.0 6.0 3.0 2.0 5.0 9.0'
result = find_repeating_sequence(function)
# repeating sequence doesn't exist
# so result should equal None in this case
示例 4:
function = '1.0 11.0 1.0 11.0 1.0 11.0 1.0 11.0 1.0 11.0 1.0 11.0 1.0 11.0 1.0 11.0'
result = find_repeating_sequence(function)
# repeating sequence = 1.0, 11.0
# so result should equal 2 in this case
到目前为止我得到的是:
def find_repeating_sequence(function):
regex = re.compile(r'(.+ .+)( )+')
match = regex.search(function)
result = match
if result == None:
print "There was no repeating sequence found!"
else:
print "Repeating sequence found!"
print match.group(1)
result = match.group(1)
return result
这适用于所有示例,因为 match.group(1)
给出了重复序列。但是由于某些原因 len(match.group(1))
不是 return 正确的数字。
如示例 1:
print match.group(1)
给出 1.0 8.0 4.0 2.0
但是 print len(match.group(1))
给出 15
它也不是 return 示例 4 的正确值:
print match.group(1)
给出 1.0 11.0 1.0 11.0 1.0 11.0 1.0 11.0
而 print len(match.group(1))
给出 35
我做错了什么?
更新:
感谢以下答案之一,我使用 len(match.group(1).split())
解决了print len(match.group(1))
问题
但是由于某些原因如果
function = 1.0 2.0 4.0 8.0 1.0 2.0 4.0 8.0 1.0 2.0 4.0 8.0 1.0 2.0 4.0 8.0
print match.group(1)
给出 1.0 2.0 4.0 8.0 1.0 2.0 4.0 8.0
而不是 1.0 2.0 4.0 8.0
match.group() returns 一个字符串,在本例中是 len() returns 字符串中的字符数。
您可以将字符串拆分 space,然后计算元素的数量
回复更新:
你的表情很贪婪,它试图找到最长的匹配。尝试 (.+? .+?)( )+?
。限定符后的问号使正则表达式变得懒惰: