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]
Advertisement
Answer
I’m gessing that :
X
is a list of lists- you are trying to find the indices of the values of
X
inX before it was last reassigned
, whichi
is not, as it can only be the last integer (50) yielded byrange
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
byX.append(None)
- replace
X = [x for x in X if min(x) != max(x)]
byvalues = [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)
byprint(f"{indices=}")
, taking advantage of f-strings