set1 = [(1,21,0),(1,20,0),(1,19,0),(1,18,0),(1,17,0)]
set2 = [(2,21,0),(2,20,0),(2,19,0),(2,18,0),(2,17,0),(2,16,0),(2,15,0)]
setNumber = [1,2]
for num in setNumber:
test = 'set' str(num)
for (a,b,c) in test:
with open('test' str(num) '.csv','a') as f:
writer = csv.writer(f)
row_data = [a,b,c]
writer.writerow(row_data)
f.close
I have two list of tuples of numbers and I want to create a loop that can go through each list but I can't make 'test' as a "range variable".
if test = set1 or test = set2, no issue, files are created.
Thanks for the help.
CodePudding user response:
You can put yout two sets (set1 and set2) in a list, and use a for loop to have your test variable go through the different sets.
set1 = [(1,21,0),(1,20,0),(1,19,0),(1,18,0),(1,17,0)]
set2 = [(2,21,0),(2,20,0),(2,19,0),(2,18,0),(2,17,0),(2,16,0),(2,15,0)]
sets = [set1, set2]
for i, test in enumerate(sets):
for (a,b,c) in test:
with open('test' str(i) '.csv','a') as f:
writer = csv.writer(f)
row_data = [a,b,c]
writer.writerow(row_data)
f.close