How to find the index of certain lists in a list of lists?

Question:

I have 50 folders containing the same file name but different contents Data_220_beta_0.1_47.0_53.0ND.csv. I am skipping certain folders which is mentioned in list I. Now, when the code scans all the remaining folders, it looks for values which are different and X = [x for x in X if min(x) != max(x)] contains the lists with distinct values. How do I identify the corresponding i values which have distinct list elements? The current and expected outputs are presented.

from functools import reduce
import pandas as pd

N=50

A=[]
X=[]

I=[8, 11, 19, 37, 40, 42]

for i in range(1,N+1):
    if i in I:
        continue
    
    file_loc =f"C:\Users\{i}\Data_220_beta_0.1_47.0_53.0ND.csv"
    df = pd.read_csv(file_loc)
    A=df["% of Nodes not visited"].to_numpy()
    
    A = [x for x in A if str(x) != 'nan']
    #print(A)
    A = [eval(e) for e in A]
    #print(A)
    
    
    X.append(A)

X = [x for x in X if min(x) != max(x)]
print("i =",i)

The current output is

i=50

The expected output is

i=[20,27,37,45,48,50]
Asked By: user20032724

||

Answers:

I’m gessing that :

  • X is a list of lists
  • you are trying to find the indices of the values of X in X before it was last reassigned, which i is not, as it can only be the last integer (50) yielded by range

Here is a reproducible example:

X = [[18, 43], [29, 2], None, [3, 3], [45], None, None, [52, 66]]

values = [x for x in X if x and min(x) != max(x)]
print(values)  # [[18, 43], [29, 2], [52, 66]]

indices = [X.index(value) for value in values]
print(indices)  # [0, 1, 7]

So, in your code, you should:

  • replace continue by X.append(None)
  • replace X = [x for x in X if min(x) != max(x)] by values = [x for x in X if x and min(x) != max(x)]
  • before printing, add indices = [X.index(value) for value in values]
  • replace print("i =",i) by print(f"{indices=}"), taking advantage of f-strings
Answered By: Laurent
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.