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

Asked

Viewed 148 times

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)]

Could I create sublists with 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)]

The number of sub-lists may vary according to the number of items in the list.

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

# 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]
  • Francisco, you are really having a hard time explaining yourself (in this question, your "how it would look" is exactly the same as the list posted immediately before; only someone who read the two questions separately will be able to understand you). This, by the way, is another important thing: if you quote another post put the link to her.

  • What you want, in fact, is to do the same separation in groups done there in your other question, only considering the pairs. That’s it?

  • I replied. Oh, in the future, take more care in the explanations. If people don’t understand your question, you just make it harder to get someone to help you. :)

  • Related: http://answall.com/q/183382/132

  • You answered yes, next time I’ll try to be clearer, sorry.

1 answer

2

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

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

The principle is exactly the same of my answer there of your other question. You just need to consider that your list now has 2 dimensions. What changes in 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:

  1. The call of np.diff now includes a parameter axis=0, to indicate that differences should be calculated by considering the first axis. This will result in the differences between x and 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]]).

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

See working on Ideone.

Browser other questions tagged

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