在Python中有效地迭代3.311031748 E + 12组合

问题描述 投票:2回答:1

我已经收集了一个庞大的口袋妖怪数据集,并开始根据我建立的比率确定“十佳团队”的目标-口袋妖怪BST(基本统计总数:平均弱点)>。对于那些关心的人,我将平均弱点计算为每种类型的口袋妖怪的弱点的总和(0.25到飞行+ 1到水+ 2到钢铁+ 4到火等),然后将其除以18(游戏中可用的类型)。

[提供一个简单的例子-由以下三个口袋妖怪组成的团队:金勒,米米丘,马格尼宗,团队比率为1604.1365384615383。

由于这些数据将用于竞技比赛,因此我删除了所有未完全进化的神奇宝贝以及传奇/神话般的神奇宝贝。到目前为止,这是我的过程:

  1. 创建完全演化的神奇宝贝团队的所有可能组合的集合
  2. 使用for循环迭代每个组合
  3. [前10个组合将自动添加到列表中
  4. 从第11个组合开始,我将当前团队迭代添加到列表中,以降序对列表进行排序,然后删除比率最低的团队。这样可以确保每次迭代后仅剩下前10名。
  5. 显然,此过程将花费很长时间。我想知道是否有一种更有效的方式来运行它。最后,请在下面查看我的代码:

import itertools
import pandas as pd

df = pd.read_csv("Downloads/pokemon.csv")  # read in csv of fully-evolved Pokemon data
# list(df)  # list of df column names - useful to see what data has been collected
df = df[df["is_legendary"] == 0]  # remove legendary pokemon - many legendaries are allowed in competitive play
df = df[['abilities',  # trim df to contain only the columns we care about
        'against_bug',
        'against_dark',
        'against_dragon',
        'against_electric',
        'against_fairy',
        'against_fight',
        'against_fire',
        'against_flying',
        'against_ghost',
        'against_grass',
        'against_ground',
        'against_ice',
        'against_normal',
        'against_poison',
        'against_psychic',
        'against_rock',
        'against_steel',
        'against_water',
        'attack',
        'defense',
        'hp',
        'name',
        'sp_attack',
        'sp_defense',
        'speed',
        'type1',
        'type2']]
df["bst"] = df["hp"] + df["attack"] + df["defense"] + df["sp_attack"] + df["sp_defense"] + df["speed"]  # calculate BSTs
df['average_weakness'] = (df['against_bug'] # calculates a Pokemon's 'average weakness' to other types
                        + df['against_dark']
                        + df['against_dragon']
                        + df['against_electric']
                        + df['against_fairy']
                        + df['against_fight']
                        + df['against_fire']
                        + df['against_flying']
                        + df['against_ghost']
                        + df['against_grass']
                        + df['against_ground']
                        + df['against_ice']
                        + df['against_normal']
                        + df['against_poison']
                        + df['against_psychic']
                        + df['against_rock']
                        + df['against_steel']
                        + df['against_water']) / 18  
df['bst-weakness-ratio'] = df['bst'] / df['average_weakness']  # ratio of BST:avg weakness - the higher the better
names = df["name"]  # pull out list of all names for creating combinations
combinations = itertools.combinations(names, 6) # create all possible combinations of 6 pokemon teams
top_10_teams = []  # list for storing top 10 teams
for x in combinations:
    ratio = sum(df.loc[df['name'].isin(x)]['bst-weakness-ratio'])  # pull out sum of team's ratio
    if(len(top_10_teams) != 10):
        top_10_teams.append((x, ratio))  # first 10 teams will automatically populate list
    else:
        top_10_teams.append((x, ratio))  # add team to list
        top_10_teams.sort(key=lambda x:x[1], reverse=True)  # sort list by descending ratios
        del top_10_teams[-1]  # drop team with the lowest ratio - only top 10 remain in list
top_10_teams

我收集了一个大型的Pokemon数据集,我的目标是根据我建立的比率确定“十佳团队”-Pokemon BST(基本统计总数):平均弱点。对于那些...

python loops bigdata combinations
1个回答
1
投票

在您的示例中,每个口袋妖怪都有bst_weakness-ratio,在计算团队价值时,您没有考虑到成员抵消彼此的弱点,而只是将6个成员的比率相加?如果是这样,最好的团队不应该是拥有6个最佳个人口袋妖怪的团队吗?我不明白为什么您需要这种组合。

© www.soinside.com 2019 - 2024. All rights reserved.