Parse numerical set for sequential numbers

Asked

Viewed 143 times

1

I have, small part, of the data below, sequence of numbers:

a = [(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15),(6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 19, 20, 23, 24, 25),(1, 2, 3, 4, 10, 11, 12, 13, 14, 16, 17, 20, 22, 24, 25),(3, 4, 7, 8, 9, 10, 13, 14, 15, 16, 18, 19, 20, 22, 24)]
, 17, 20, 22, 24, 25],[3, 4, 7, 8, 9, 10, 13, 14, 15, 16, 18, 19, 20, 22, 24]]

I arrived in the analysis below:

sequencia = [(15,), (10, 2, 3), (4, 5, 2, 1, 1, 2), (2, 4, 4, 3, 1, 1)]
seqMax = [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
 [6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
 [10, 11, 12, 13, 14],
 [7, 8, 9, 10]]

That is to say:

  • First list of numbers: (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15), has 15 sequential numbers: (15,) and the largest sequence found is itself;

  • Second list of numbers: (6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 19, 20, 23, 24, 25), has 3 sequences: (10, 2, 3) where 10 = 6, 7, 8, 9, 10, 11, 12, 13, 14, 15; 2 = 19, 20 and 3 = 23, 24, 25; and the largest sequence is 10: 6, 7, 8, 9, 10, 11, 12, 13, 14, 15.

I have set up a function, complete code below, but I would like to validate with you if there is an easier way to encode:

import pandas as pd
a = [(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15),(6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 19, 20, 23, 24, 25),(1, 2, 3, 4, 10, 11, 12, 13, 14, 16, 17, 20, 22, 24, 25),(3, 4, 7, 8, 9, 10, 13, 14, 15, 16, 18, 19, 20, 22, 24)]
def sequencia_linha(x):
    a = list(x)
    ab = []
    sequencia=[]
    seqMaxInterno=[]
    for n in a:
        if len(ab)>0:
            if max(ab)+1 == n:
                ab.append(n)
                cont+=1
            else:
                if len(ab) > len(seqMaxInterno):
                    seqMaxInterno=ab
                sequencia.append(cont)
                ab=[]
                ab.append(n)
                cont=1
        else:
            ab.append(n)
            cont=1
    if len(ab) > len(seqMaxInterno):
        seqMaxInterno=ab
    sequencia.append(cont)
    seqMax.append(seqMaxInterno)
    return tuple(sequencia)

seqMax=[]
sequencia=[]
for x in a:
    sequencia.append(sequencia_linha(x))
  • Any suggestions?

  • How do you use this function? I tried to put your line a = [ blablabla ] which is in your question and then print(sequencia(a)). All this right after the end of your code. But, it gave error in if max(ab) + 1 == n: saying that TypeError: can only concatenate list (not "int") to list.

  • It was not very clear what logic should be made to generate the output. What exactly represents each value of b?

  • Victor and Anderson, I fixed the code so it’ll be easier to interpret.

1 answer

0

I think it’s a simpler and easier solution to understand:

a = [(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15), (6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 19, 20, 23, 24, 25),
 (1, 2, 3, 4, 10, 11, 12, 13, 14, 16, 17, 20, 22, 24, 25), (3, 4, 7, 8, 9, 10, 13, 14, 15, 16, 18, 19, 20, 22, 24)]

def sequencia_por_linha(linha):
    seq = []
    seq_list =[[]]
    prev = None
    i = 0   # Contador de números em sequência
    j = 0   # Contador de listas de sequências

    for n in linha:
        if prev == None:
            seq_list[j].append(n)

        elif n == prev + 1:
            i += 1
            seq_list[j].append(n)

        else:
            seq.append(i + 1)
            seq_list.append([n])
            i = 0
            j += 1

        prev = n

    seq.append(i + 1)

    seq_max_int = seq_list[seq.index(max(seq))]    # Retorna a lista mais longa de números em sequência

    return tuple(seq), seq_max_int

seqMax = []
sequencia = []

for linha in a:

    seq, seq_max_int = sequencia_por_linha(linha)

    sequencia.append(seq)
    seqMax.append(seq_max_int)

print(sequencia)
print(seqMax)

outworking:

[(15,), (10, 2, 3), (4, 5, 2, 1, 1, 2), (2, 4, 4, 3, 1, 1)]
[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], [6, 7, 8, 9, 10, 11, 12, 13, 14, 15], [10, 11, 12, 13, 14], [7, 8, 9, 10]]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.