简体   繁体   English

Python:查找浮点数字符串中重复数字序列的长度

[英]Python: Finding length of the repeating sequence of numbers in a string of floats

I am trying to write a function which takes in a list of floats and determines whether or not there is a repeating sequence of number within that list. 我正在尝试编写一个函数,该函数接受一个浮点数列表,并确定该列表中是否存在重复的数字序列。 If there is a repeating sequence then it counts the numbers in that sequence. 如果存在重复序列,则它将计算该序列中的数字。

These examples are what I want my function to do 这些示例是我希望函数执行的操作

Example 1: 范例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

Example 2: 范例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

Example 3: 范例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

Example 4: 范例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

So far what I have got is: 到目前为止,我得到的是:

def find_repeating_sequence(function):
        regex = re.compile(r'(.+ .+)( \1)+')
        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

This works for all examples in the sense that match.group(1) gives the repeating sequence. match.group(1)给出重复序列的意义上,这适用于所有示例。 However for some reason len(match.group(1)) does not return the correct number. 但是由于某种原因, len(match.group(1))不能返回正确的数字。

Like for Example 1: 类似于示例1:

print match.group(1) gives 1.0 8.0 4.0 2.0 print match.group(1)给出1.0 8.0 4.0 2.0

But print len(match.group(1)) gives 15 但是print len(match.group(1))给出15

Also it does not return the correct value for Example 4: print match.group(1) gives 1.0 11.0 1.0 11.0 1.0 11.0 1.0 11.0 而且它不会返回示例4的正确值: print match.group(1)给出1.0 11.0 1.0 11.0 1.0 11.0 1.0 11.0

And print len(match.group(1)) gives 35 然后print len(match.group(1))得到35

What am I doing wrong? 我究竟做错了什么?

UPDATE: 更新:

Thanks to one of the answers below I have solved the print len(match.group(1)) issue by using len(match.group(1).split()) 感谢下面的答案之一,我通过使用len(match.group(1).split())解决了print len(match.group(1))问题

However for some reason if 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) gives 1.0 2.0 4.0 8.0 1.0 2.0 4.0 8.0 and not 1.0 2.0 4.0 8.0 但是由于某种原因,如果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 a string and in this case len() returns the number of characters in the string. match.group()返回一个字符串,在这种情况下,len()返回该字符串中的字符数。

you could split your string by space, and then count the number of elements 您可以按空格分割字符串,然后计算元素数

Answer to the update: your expression is greedy, it tries to find the longest match. 更新的答案:您的表情很贪婪,它试图找到最长的匹配项。 Try (.+? .+?)( \\1)+? 尝试(.+? .+?)( \\1)+? . The questionmark after a qualifier makes the regex lazy: 限定词后的问号使正则表达式变得懒惰:

https://regex101.com/r/1w7nxD/1 https://regex101.com/r/1w7nxD/1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM