擅长:python、mysql、java
<p>这实际上只是循环中的循环:</p>
<pre><code>for each player
for each other player
if no value in player's values is in other player's values, add the pair
</code></pre>
<p>最后一行中当然有一个隐式循环(实际上,有两个,因为列表中的“is in”本身也有一个循环,但我们还是忘了这一个,因为它只是一个次要的性能问题,而不是概念问题)</p>
<p>如果要使第三个循环显式化:</p>
<pre><code>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
</code></pre>
<hr/>
<p>那么,如何将其转换为Python呢</p>
<p>好吧,“对于每个玩家”只是<code>for player in team</code>——或者<code>for player, values in team.items()</code>可能会为您以后节省一些工作</p>
<p>那么“为对方球员”又是一回事了(当然,这意味着“玩家”可以作为一个“其他玩家”来比较,这是不必要的,但它不会损害任何东西,除了一个小的性能成本比较某人和他自己,这将失败的第一次检查。)</p>
<p>那么“如果玩家的价值观中没有价值观是其他玩家的价值观”就是<code>if not any(value in other_values for value in player_values)</code>。您可以通过将<code>other_values</code>转换为一个集合来加快这个过程,但是考虑到列表有多短,可能没有必要这样做</p>
<p>最后,“add the pair”只是指<code>pairs.append((player, other))</code>,或者<code>yield (player, other)</code>,如果您理解生成器的话</p>
<p>希望这足够你自己写了</p>