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.
– Luiz Vieira
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?
– Luiz Vieira
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. :)
– Luiz Vieira
Related: http://answall.com/q/183382/132
– Victor Stafusa
You answered yes, next time I’ll try to be clearer, sorry.
– Francisco Gerson