Home > Mobile >  Printing indices of list containing different elements in Python
Printing indices of list containing different elements in Python

Time:09-23

I have a list X. I want to print indices of X which contain different list elements. For instance, X[0] has some different elements, X[1] has all the same elements and X[2] has again different elements. I present the expected output.

X=[[[46.36363636363636],
 [35.90909090909091],
 [31.363636363636367],
 [25.454545454545453],
 [24.545454545454547],
 [24.545454545454547],
 [24.545454545454547],
 [24.545454545454547],
 [24.545454545454547],
 [24.545454545454547]],

   [[46.36363636363636],
 [46.36363636363636],
 [46.36363636363636],
 [46.36363636363636],
 [46.36363636363636],
 [46.36363636363636],
 [46.36363636363636],
 [46.36363636363636],
 [46.36363636363636],
 [46.36363636363636]],
    
   [[46.36363636363636],
    [39.09090909090909],
    [37.27272727272727],
    [35.0],
    [33.18181818181819],
    [32.72727272727273],
    [32.27272727272727],
    [32.27272727272727],
    [32.27272727272727],
    [32.27272727272727]]]

for i in range(0,len(X)):
    X = [x for x in X if min(x) != max(x)]

The expected output is

i=[0,2]

CodePudding user response:

Loop over every list in your list, and check if each list contains only the same elements. If they don't, return the index of the list.

res = [x for x, y in enumerate(X) if not all(e == y[0] for e in y)]
print(res)

>>> [0, 2]

CodePudding user response:

You are looking for enumerate.

i = [index for index, sublist in enumerate(X) if min(sublist) != max(sublist)]
  • Related