统计分析python_python统计分析

你也许可以用递归来解决这个问题。下面显示了基本的轮廓,但忽略了一些细节,比如一个团队由一定数量的特定类型的球员组成。在players=[{'name':'A','score':5,'cost':10},

{'name':'B','score':10,'cost':3},

{'name':'C','score':6,'cost':8}]

def player_cost(player):

return player['cost']

def player_score(player):

return player['score']

def total_score(players):

return sum(player['score'] for player in players)

def finance_team_recurse(budget, available_players):

affordable_players=[]

for player in available_players:

if player_cost(player)<=budget:

# Since we've ordered available players, the first player appended

# will be the one with the highest score.

affordable_players.append(player)

result=[]

if affordable_players:

candidate_player=affordable_players[0]

other_players=affordable_players[1:]

# if you include candidate_player on your team

team_with_candidate=finance_team_recurse(budget-player_cost(candidate_player),

other_players)

team_with_candidate.append(candidate_player)

score_of_team_with_candidate=total_score(team_with_candidate)

if score_of_team_with_candidate>total_score(other_players):

result=team_with_candidate

else:

# if you exclude candidate_player from your team

team_without_candidate=finance_team_recurse(budget, other_players)

score_of_team_without_candidate=total_score(team_without_candidate)

if score_of_team_with_candidate>score_of_team_without_candidate:

result=team_with_candidate

else:

result=team_without_candidate

return result

def finance_team(budget, available_players):

tmp=available_players[:]

# Sort so player with highest score is first. (Greedy algorithm?)

tmp.sort(key=player_score, reverse=True)

return finance_team_recurse(budget,tmp)

print(finance_team(20,players))

# [{'score': 6, 'cost': 8, 'name': 'C'}, {'score': 10, 'cost': 3, 'name': 'B'}]

^{pr2}$

所以总共有20*20260275*20260275*4950=40637395564486875000L

teams字典中的项。这会占用大量内存。在for gk in itertools.combinations(G,1):

for de in itertools.combinations(D,4):

for mi in itertools.combinations(M,4):

for st in itertools.combinations(S,2):

#Don't collect the results into a dict.

#That's what's killing you (memory-wise).

#Just compute the cost and

#Just print the result here.

PS.40637395564486875000L的顺序是10**19。假设你的程序每秒可以处理10**6个组合,这个程序大约需要130万年才能完成。。。在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值