我基本上有n个玩家,每个玩家我都在计算它们与其他玩家的相似程度,并按顺序或相似性存储它们。
我正在使用Pearson Correlation Coefficient来衡量任何两个玩家之间的相似性。现在基本上我想要一个数据结构,我可以在其中存储每个玩家的信息,包括它们与所有其他玩家之间的相似性值。所以对于player1,我想保留其与所有其他玩家的相似度值列表。因此我想到创建一个数组列表,每个索引都代表玩家,例如索引0将是玩家1,依此类推。现在在索引中我们将有一个字典数组。每个字典都有playerId和值的关键字作为自身与我们所指的玩家之间的相似性。
Player1 --> [[player2:0.0], [player3:0.5]]
so we would have one array where we have player1,player2,player3
and then in each index we have information as showed above.
答案 0 :(得分:2)
让我们保持简单。您只需要一个字典和两个辅助方法,如下所示
这考虑了播放器1和播放器之间的相似性。无论在放置期间的顺序如何,player2保持不变。得到。
similarities = {}
def set_sim(players, sim):
similarities[tuple(sorted(players))] = sim
def get_sim(players):
return similarities.get(tuple(sorted(players)))
以下是如何使用它们
>>> set_sim(['Player3', 'Player1'], 2)
>>> set_sim(['Player1', 'Player2'], 3)
>>> set_sim(['Player2', 'Player3'], 3)
>>> get_sim(['Player3','Player2'])
3
>>> similarities
{('Player1', 'Player2'): 3, ('Player2', 'Player3'): 3, ('Player1', 'Player3'): 2}
如果您需要寻找其他玩家,辅助方法应该再次轻松。
def get_other_players(player):
for pair in similarities.keys():
try:
other_player = pair[(pair.index(player)+1)%2]
print other_player, "=", similarities[pair]
except ValueError:
pass
日志:
>>> set_sim(['Player9','Player4'], .02)
>>> set_sim(['Player3','Player4'], .8)
>>> set_sim(['Player12','Player4'], 1.5)
>>> get_other_players('Player4')
Player9 = 0.02
Player3 = 0.8
Player12 = 1.5
答案 1 :(得分:1)
我的英语很差,所以我认为你的意思是
player1 = {'player2': 0.0, 'player3': 0.5}
for x in player1:
print(x, ' is ', player1[x])
答案 2 :(得分:1)
是吗?
from collections import namedtuple
p1 = namedtuple('p1', ['p2', 'p3'])
p2 = namedtuple('p2', ['p1', 'p3'])
p3 = namedtuple('p3', ['p1', 'p2'])
p1.p2 = 0.4
p1.p3 = 1.1
p2.p1 = 1.5
p2.p3 = 0.0
p3.p1 = 0.5
p3.p2 = 1.0
total = (p1, p2, p3)
for i in total:
print('* {}'.format(i.__name__))
for i2 in i._fields:
print('{}->{}: {}'.format(i2, i.__name__, eval(i2 + '.' + i.__name__)))
也许有一种更优雅的方式来做到这一点,但它有效...... = P
答案 3 :(得分:0)
您可能正在寻找dictionary
players = {
"player1": 0.0,
"player2": 0.0,
"player3": 0.5,
}
然后您可以将其作为
进行访问players["player1"]
或用
更改players["player2"] = 0.7
添加新
players["player4"] = 0.1
并查看全部
print(players)
答案 4 :(得分:0)
similarity={
'player1': { 'player2': 0.0,
'player3': 0.5 },
'player2': { 'player1': 0.0,
'player3': 0.8 },
'player3': { 'player1': 0.5,
'player2': 0.8 }
}
print(similarity['player1'])
# will give you: {'player2': 0.0, 'player3': 0.5}
print(similarity['player1']['player3'])
# will give you 0.5
更强大的解决方案:
class Sim:
__data = None
def __init__(self):
self.__data = dict()
def set(self, player1, player2, similarity):
if not self.__data.has_key(player1):
self.__data[player1] = dict()
if not self.__data.has_key(player2):
self.__data[player2] = dict()
self.__data[player1][player2] = similarity
self.__data[player2][player1] = similarity
def get(self, player1, player2):
return self.__data[player1][player2]
def __str__(self):
return str(self.__data)
def __repr__(self):
return repr(self.__data)
example=Sim()
example.set('player1', 'player2', 0)
example.set('player1', 'player3', 0.5)
print(example)
# will print:
# {'player2': {'player1': 0}, 'player3': {'player1': 0.5}, 'player1': {'player2': 0, 'player3': 0.5}}