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
x
29
29
1
from functools import reduce
2
import pandas as pd
3
4
N=50
5
6
A=[]
7
X=[]
8
9
I=[8, 11, 19, 37, 40, 42]
10
11
for i in range(1,N+1):
12
if i in I:
13
continue
14
15
file_loc =f"C:\Users\{i}\Data_220_beta_0.1_47.0_53.0ND.csv"
16
df = pd.read_csv(file_loc)
17
A=df["% of Nodes not visited"].to_numpy()
18
19
A = [x for x in A if str(x) != 'nan']
20
#print(A)
21
A = [eval(e) for e in A]
22
#print(A)
23
24
25
X.append(A)
26
27
X = [x for x in X if min(x) != max(x)]
28
print("i =",i)
29
The current output is
JavaScript
1
2
1
i=50
2
The expected output is
JavaScript
1
2
1
i=[20,27,37,45,48,50]
2
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:
JavaScript
1
8
1
X = [[18, 43], [29, 2], None, [3, 3], [45], None, None, [52, 66]]
2
3
values = [x for x in X if x and min(x) != max(x)]
4
print(values) # [[18, 43], [29, 2], [52, 66]]
5
6
indices = [X.index(value) for value in values]
7
print(indices) # [0, 1, 7]
8
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