Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Skipping certain folders in Python

Is there a way to skip certain folders? In the present code, I specify number of folders N=20 i.e. the code analyzes all folders named 1,2,...,20. However, in this range, I want to skip certain folders, say 10,15. Is there a way to do so?

import pandas as pd
import numpy as np
from functools import reduce
import statistics

A=[]
X=[]

N=20   #Number of folders

for i in range(1,N):
    file_loc = f"C:\\Users\\USER\\OneDrive - Technion\\Research_Technion\\Python_PNM\\Sept7_2022\\220\\beta_1e-1\\Var_1\\10 iterations\\{i}\\Data_220_beta_1e-1_48.25_51.75ND.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']
    
    A = [eval(e) for e in A]
    
    X.append(A)

#print(X)
X = [x for x in X if min(x) != max(x)]
A=[reduce(lambda x,y: x+y, v) for v in zip(*X)]
print("A =",A)

Mean1=[]
Std1=[]

for i in range(0,len(A)):
        Mean=statistics.mean(A[i]) 
        Std=statistics.stdev(A[i]) 
        
        Mean1.append(Mean)
        Std1.append(Std)
        
print("mean =",*Mean1,sep='\n')
print("std =",*Std1,sep='\n')

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Just add a test to skip the unwanted numbers:


N = 20   # Number of folders
skip = {10,15} # using a set for efficiency

for i in range(1,N):
    if i in skip:
        continue
    # rest of code

NB. If you want to include 20, you should use range(1, N+1).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading