Skip to content
Advertisement

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

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.

JavaScript

The current output is

JavaScript

The expected output is

JavaScript

Advertisement

Answer

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:

JavaScript

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
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement