I have a bunch of files to iterate over to find the best combination, and would like some advice on how to do it. I have to put together a mixed-age "relay" team. We need (exactly) one person from each age. But, we have a max weight that we can't go over. We know roughly how much each person will score -- the highest total score wins.
I have all the peeps in files by their age with their name, weight and score, eg the 5.csv file has all the 5yr olds (name,weight,score)
JonnyM,54,20
SallyR,35,18
MeganP,33,25
...
the 6.csv has the 6yr olds with the same format
DaveL,53,30
NancyP,40,28
...
etc up to 20.csv.
I suppose I could do this with a lot of typing:
import csv
maxweight=5000
bestscore=0
bestcombo=[]
f5 = csv.reader(open("5.csv", "r"), delimiter=',')
f6 = csv.reader(open("6.csv", "r"), delimiter=',')
...
f20 = csv.reader(open("20.csv", "r"), delimiter=',')
for name5,weight5,score5 in 5f:
for name6,weight6,score6 in 6f:
...
...(and a lot more)
for name20,weight20,score20 in 20f:
if((weight5+weight6+...weight20)<=maxweight):
if((score5+score6+...score20)>bestscore):
bestcombo=[name5,name6,...name20]
But, there has got to be a better way. I'm sure its obvious, but I'm also pretty new to python.