How to create sublists with pairs of elements (x, y), where the first of them (x) is in a sequence?

1

Friends, imagine a list of pairs of elements (x, y):

  

lista = [(13, 38), (14, 35), (14, 36), (15, 33), (15, 34), (15, 35), (15, 36), (18, 31), (18, 32), (19, 33), (22, 34), (22, 29), (23, 30), (23, 31), (24, 32)]

Would I have to create sublists with the pairs of elements in which the first element (x) is in sequence? For example, in this case it would be:

  

lista = [[(13, 38), (14, 35), (14, 36), (15, 33), (15, 34), (15, 35), (15, 36)], [(18, 31), (18, 32), (19, 33)], [(22, 34), (22, 29), (23, 30), (23, 31), (24, 32)]]

  

Grupo #0: [(13, 38), (14, 35), (14, 36), (15, 33), (15, 34), (15, 35), (15, 36)] Grupo #1: [(18, 31), (18, 32), (19, 33)] Grupo #2: [(22, 34), (22, 29), (23, 30), (23, 31), (24, 32)]

Since the number of sublists can vary, depending on the number of elements in the list.

A member here in the forum passed the code below , I initially in another post I did not express myself correctly and I went through a simple list, it worked very well, the question is that I will work with pairs of elements, who can help thanks :

# Sua lista original

lista = [1, 2, 2, 3, 3, 3, 6, 6, 7, 11, 12, 12, 13, 14, 14]

# Importa a biblioteca NumPy

import numpy as np

# Separa em grupos usando como índices da separação os locais onde ocorre uma
# diferença entre o item atual e o próximo maior do que 1

grupos = np.split(lista, [i+1 for i,j in enumerate(np.diff(lista)) if j > 1])

# Imprime os grupos produzidos

for i, g in enumerate(grupos):
    print('Grupo #{}: {}'.format(i, g.tolist()))

It produces the following output:

Grupo #0: [1, 2, 2, 3, 3, 3]
Grupo #1: [6, 6, 7]
Grupo #2: [11, 12, 12, 13, 14, 14]
    
asked by anonymous 11.02.2017 / 13:13

1 answer

2
  

I I suggested that you open another   question   because I understood that the problem was another. But it is essentially the   same as your question   original, you just committed the   error of not making it clear there that its structure was not   one-dimensional.

     

I considered marking this as a duplicate of that and asking you to   correct the question there, but it would invalidate another answer that you already   has. So I'll reply here again.

The principle is exactly the same from my answer there from your other question . You just need to consider that your list now has 2 dimensions. What changes in the code is this line:

grupos = np.split(lista, [i+1 for i,j in enumerate(np.diff(lista, axis=0)) if j[0] > 1])

The differences are:

  • The np.diff call now includes a axis=0 parameter, to indicate that the differences should be calculated considering the first axis. This will result in the differences between x and between y, also in a two-dimensional list ( [[ 1, -3], [ 0, 1], [ 1, -3], [ 0, 1], [ 0, 1], [ 0, 1], [ 3, -5],[ 0, 1], [ 1, 1], [ 3, 1], [ 0, -5], [ 1, 1], [ 0, 1], [ 1, 1]] ).

  • The check now compares j[0] > 1 instead of comparing j > 1 , since j is now a tuple with two numbers (x, y), and position 0 is the value of x. / p>

  • See working at Ideone .

        
    11.02.2017 / 16:26