<p><strong>编辑:更正,感谢@Blckknght的评论。</strong></p>
<p>根据您的输入数据:</p>
<pre><code>upList = ("up1","up2","up3")
downList = ("down1","down2","down3")
</code></pre>
<p>首先要创建分子x分母的所有排列。可从<a href="https://docs.python.org/3/library/itertools.html?highlight=itertools.combination#itertools.combinations" rel="nofollow noreferrer">^{<cd1>}</a>获得。你知道吗</p>
<pre><code>from itertools import product
ratios = product(upList, downList)
</code></pre>
<p>接下来,您需要从产品中找到两个不同项的所有组合。这是一个2-组合,可以通过<a href="https://docs.python.org/3/library/itertools.html?highlight=itertools.combination#itertools.combinations" rel="nofollow noreferrer">^{<cd2>}</a>得到。你知道吗</p>
<pre><code>from itertools import combinations
ratio_pairs = combinations(ratios, 2)
</code></pre>
<p>但是你想把组合限制在两个项目不共享同一分子,也不共享同一分母的情况下。这是一个过滤列表:</p>
<pre><code>distinct_ratio_pairs = [ (p1,p2) for p1,p2 in ratio_pairs if p1[0] != p2[0] and p1[1] != p2[1] ]
for drp in distinct_ratio_pairs:
print(drp)
</code></pre>
<p>输出:</p>
<pre><code>(('up1', 'down1'), ('up2', 'down2'))
(('up1', 'down1'), ('up2', 'down3'))
(('up1', 'down1'), ('up3', 'down2'))
(('up1', 'down1'), ('up3', 'down3'))
(('up1', 'down2'), ('up2', 'down1'))
(('up1', 'down2'), ('up2', 'down3'))
(('up1', 'down2'), ('up3', 'down1'))
(('up1', 'down2'), ('up3', 'down3'))
(('up1', 'down3'), ('up2', 'down1'))
(('up1', 'down3'), ('up2', 'down2'))
(('up1', 'down3'), ('up3', 'down1'))
(('up1', 'down3'), ('up3', 'down2'))
(('up2', 'down1'), ('up3', 'down2'))
(('up2', 'down1'), ('up3', 'down3'))
(('up2', 'down2'), ('up3', 'down1'))
(('up2', 'down2'), ('up3', 'down3'))
(('up2', 'down3'), ('up3', 'down1'))
(('up2', 'down3'), ('up3', 'down2'))
</code></pre>