用Python代表团队的最佳数据结构

时间:2017-10-22 15:50:20

标签: python data-structures

我有一个Python字典,列出了几场比赛的团队成员

match={'match1':[a,b,c,x,y,z],'match2':[a,y,z,k,m,o], 'match3':[c,x,a,k,l,m]}

我需要计算每个团队成员和其他每个团队成员所进行的比赛次数。

例如,a-c = 2,a-o = 1

表示此数据的最佳数据结构是什么,以便我可以轻松计算每个团队成员组合的总匹配数?

2 个答案:

答案 0 :(得分:0)

你可以试试这个。每场比赛都是一组。您有集合列表,然后按您的条件过滤它们,计算集合。

s1 = frozenset(['a','b','c','x','y','z'])
s2 = frozenset(['a','y','z','k','m','o'])
s3 = frozenset(['c','x','a','k','l','m'])

def find_common_matches(first_player, second_player, *sets):
        return sum(1 for x in frozenset(*sets) if first_player in x and second_player in x)

lst = [s1, s2, s3]
count = find_common_matches('a', 'c',lst)
print(count)

答案 1 :(得分:0)

我强烈建议您使用pandas数据框,这不是因为您拥有,而是因为它真的是一个很棒的软件包。

import pandas as pd

#Define your columns and row values in a dictionary. Columns = Matches, Rows 
#= Players
matches={'match1':['a','b','c','x','y','z'],
         'match2':['a','y','z','k','m','o'], 
         'match3':['c','x','a','k','l','m']}

#Generate the dataframe
df = pd.DataFrame(matches)

#Shows you which players played in what match.
player_matches = df.apply(pd.Series.value_counts)

#Adds a new column which is defined as the total number of matches for each 
#player.
player_matches['total'] = player_matches.sum(axis=1)

#Pairing matches can be done. Here's a generalized method for that
def pairCount(a, b, df):
    pairs = 0
    for col in df.columns.tolist():
        if df.loc[a,col] == 1.0 and df.loc[b,col] == 1.0:
            pairs += 1
    return pairs

#For example, a-c=2, a-o=1
ac = pairCount('a', 'c', player_matches)
print(ac)

ao = pairCount('a', 'o', player_matches)
print(ao)

pandas套餐很棒,绝对值得学习和玩弄。你可以做更多!