Skipping certain folders in Python

Question:

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')
Asked By: user19862793

||

Answers:

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).

Answered By: mozway
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.