lundi 20 avril 2020

Python: Selecting repeated elements (list in a list) and put it in an array?

How to make from this:

[[1, 0, 0], [1, 0, 0], [1, 0, 0], [1, 0, 1], [1, 0, 1], [1, 0, 1]]

to this?

array([[1, 0, 0],
       [1, 0, 1]])

I tried this but it only works with 2 repeated elements. I want it to work for any number of repeated elements.

finalRes = []

for i in range(len(resList)-1):
    if resList[i] == resList[i+1]:
        finalRes.append(resList[i])

finalRes --> [[1, 0, 0], [1, 0, 0], [1, 0, 1], [1, 0, 1]]

Aucun commentaire:

Enregistrer un commentaire