简体   繁体   English

从字典python中的唯一值创建对

[英]creating pairs from unique values in a dictionary python

i'm trying to get a firm grasp of iterating through dictionaries, especially when values are of unequal lengths (this is causing the most errors for me). 我试图牢固地理解字典的迭代,尤其是当值的长度不相等时(这对我造成了最大的错误)。 I'm actually trying to run a script for my basketball program to find pairs. 我实际上正在尝试为我的篮球程序运行脚本以查找对。 here is a snippet of the team: 这是团队的摘要:

team = {'Bryan': ['m', 'pg','sg','sf'], 'Steve': ['m', 'pg','sf','c'], 'Suzy': ['f,','c','pf'], 'Jen': ['f','pf']}

basically, i set up my dictionaries so that if no key has any values in common within the list, they are a pair. 基本上,我设置了字典,以便如果键在列表中没有任何共同的值,则它们是一对。

the output that i've been trying to get is: 我一直试图获得的输出是:

[('Suzy','Bryan'), ('Jen','Bryan'), ('Jen','Steve')]

so Suzy and Bryan's values within the list have nothing in common. 因此列表中Suzy和Bryan的值没有共同之处。 same for the other two. 其他两个相同。 very interested to see ways to approach the problem. 非常有兴趣了解解决问题的方法。

import itertools
def find_matches(team):
   for player1,player2 in itertools.combinations(team.keys(),2):
       if not set(team[player1]).intersection(team[player2]):
           yield (player1,player2)

team = {'Bryan': ['m', 'pg','sg','sf'], 'Steve': ['m', 'pg','sf','c'], 'Suzy': ['f,','c','pf'], 'Jen': ['f','pf']}           
print list(find_matches(team))

is probably how I would do it ... 大概就是我会怎么做...

This is really just a matter of loops inside loops: 这实际上仅是循环内部循环的问题:

for each player
    for each other player
        if no value in player's values is in other player's values, add the pair

That last line has an implicit loop in it, of course (actually, two, because "is in" on a list itself has a loop in it, but let's forget that one, as it's just a minor performance issue, not a conceptual issue). 最后一行当然有一个隐式循环(实际上是两个,因为列表中的“位于”本身也有一个循环,但我们忘了一个,因为它只是一个较小的性能问题,而不是概念问题) )。

If you want to make the third loop explicit: 如果要使第三个循环更明确:

for each player
    for each other player
        for each value in player's values
            if value in other player's values, break
        else add the pair

So, how do you translate that to Python? 那么,如何将其转换为Python?

Well, "for each player" is just for player in team —or for player, values in team.items() may save you a bit of work later. 好吧,“对于每个玩家”仅for player in team for player, values in team.items()或者for player, values in team.items()可能会for player, values in team.items()您节省一些工作。

Then "for each other player" is the same thing again. 那么“对于其他玩家”又是同一回事。 (Of course that means that "player" can come up as an "other player" to compare to, which is unnecessary—but it doesn't hurt anything, except a minor performance cost comparing someone to himself, which will fail on the first check.) (当然,这意味着“玩家”可以作为“其他玩家”来比较,这是不必要的,但它并不会对任何人造成伤害,除了与某人进行比较而产生的较小的性能成本外,这首先会失败)校验。)

Then "if no value in player's values is in other player's values" is just if not any(value in other_values for value in player_values) . 那么,“如果玩家的值中if not any(value in other_values for value in player_values)值是其他玩家的值中”,就if not any(value in other_values for value in player_values) You can make this faster by converting the other_values to a set, but there's probably no need for that given how short your lists are. 您可以通过将other_values转换为集合来使其更快,但是考虑到列表的长度,可能不需other_values

Finally, "add the pair" just means pairs.append((player, other)) , or yield (player, other) if you understand generators. 最后,如果您了解生成器,“添加对”仅表示pairs.append((player, other))yield (player, other)

Hopefully that's enough to write it yourself. 希望这足以自己编写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM