6

我有一个列表列表,每个列表都有一个重复序列。我正在尝试计算列表中重复整数序列的长度:

list_a = [111,0,3,1,111,0,3,1,111,0,3,1] 

list_b = [67,4,67,4,67,4,67,4,2,9,0]

list_c = [1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,23,18,10]

哪个会返回:

list_a count = 4 (for [111,0,3,1])

list_b count = 2 (for [67,4])

list_c count = 10 (for [1,2,3,4,5,6,7,8,9,0])

欢迎任何建议或提示。我现在正在尝试使用 re.compile 来解决它,但是它并不完全正确。

4

3 回答 3

11

通过在序列长度的 2 到一半之间进行迭代来猜测序列长度。如果没有发现模式,则默认返回 1。

def guess_seq_len(seq):
    guess = 1
    max_len = len(seq) / 2
    for x in range(2, max_len):
        if seq[0:x] == seq[x:2*x] :
            return x

    return guess

list_a = [111,0,3,1,111,0,3,1,111,0,3,1] 
list_b = [67,4,67,4,67,4,67,4,2,9,0]
list_c = [1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,23,18,10]

print guess_seq_len(list_a)
print guess_seq_len(list_b)
print guess_seq_len(list_c)
print guess_seq_len(range(500))   # test of no repetition

这给出了(如预期的那样):

4
2
10
1

根据要求,此替代方案给出了最长的重复序列。因此它将为 list_b 返回 4。唯一的变化guess = xreturn x

def guess_seq_len(seq):
    guess = 1
    max_len = len(seq) / 2
    for x in range(2, max_len):
        if seq[0:x] == seq[x:2*x] :
            guess = x

    return guess
于 2012-07-08T18:58:25.700 回答
0

我采用了Maria更快、更符合 stackoverflow 的答案,并使其首先找到最大的序列:

def guess_seq_len(seq, verbose=False):
    seq_len = 1
    initial_item = seq[0]
    butfirst_items = seq[1:]
    if initial_item in butfirst_items:
        first_match_idx = butfirst_items.index(initial_item)
        if verbose:
            print(f'"{initial_item}" was found at index 0 and index {first_match_idx}')
        max_seq_len = min(len(seq) - first_match_idx, first_match_idx)
        for seq_len in range(max_seq_len, 0, -1):
            if seq[:seq_len] == seq[first_match_idx:first_match_idx+seq_len]:
                if verbose:
                    print(f'A sequence length of {seq_len} was found at index {first_match_idx}')
                break
    
    return seq_len
于 2021-03-11T13:21:08.893 回答
-1

这对我有用。

def repeated(L):
    '''Reduce the input list to a list of all repeated integers in the list.'''
    return [item for item in list(set(L)) if L.count(item) > 1]

def print_result(L, name):
    '''Print the output for one list.'''
    output = repeated(L)
    print '%s count = %i (for %s)' % (name, len(output), output)

list_a = [111, 0, 3, 1, 111, 0, 3, 1, 111, 0, 3, 1]
list_b = [67, 4, 67, 4, 67, 4, 67, 4, 2, 9, 0]
list_c = [
    1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2,
    3, 4, 5, 6, 7, 8, 9, 0, 23, 18, 10
]

print_result(list_a, 'list_a')
print_result(list_b, 'list_b')
print_result(list_c, 'list_c')

Python 的set()函数会将列表转换为集合,这是一种只能包含任何给定值之一的数据类型,很像代数中的集合。我将输入列表转换为一个集合,然后再转换回一个列表,将列表缩减为只有它的唯一值。然后,我测试了这些值中的每一个的原始列表,看它是否多次包含该值。我返回了所有重复项的列表。其余代码仅用于演示目的,以表明它有效。

编辑:语法突出显示不喜欢我的文档字符串中的撇号。

于 2012-07-08T19:22:22.967 回答