我有一堆文件要迭代,以找到最佳的组合,并希望得到一些建议,如何做到这一点。我必须把 一起组成一个混合年龄的“接力”团队。我们每个年龄需要一个人。但是,我们有一个不能超过的最大重量。我们知道 大概每个人会得到多少分——最高的总分获胜。你知道吗
我有他们的年龄,他们的名字,体重和分数,如5.csv文件中的所有5岁的孩子(姓名,体重,分数)的所有窥视文件
JonnyM,54,20
SallyR,35,18
MeganP,33,25
...
6.csv中的6岁儿童具有相同的格式
DaveL,53,30
NancyP,40,28
...
etc高达20.csv。你知道吗
我想我可以用大量的打字来完成:
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]
但是,还有更好的办法。我确信这是显而易见的,但我对python也是相当陌生的。你知道吗
这里有一个解决方案,已经留下足够的意见,以便于理解。您可以用基于文件的读取来替换硬编码的数据列表
您可以使用
itertools.product
。还应使用contextlib.ExitStack
关闭文件:这是未经测试的(因为我没有访问文件的权限,也懒得合成有效的伪数据),所以如果出现错误,请告诉我。而且,这种方法相当幼稚,因此计算成本很高。你知道吗
相关问题 更多 >
编程相关推荐