擅长:python、mysql、java
<p>使用itertools置换,过滤结果并消除重复项:</p>
<pre><code>import itertools
currency_pairs = ['BCH/BTC', 'BCH/ETH', 'DASH/USD', 'BTC/USDT', 'ETH/BTC']
set_triplets = set()
for triplet in itertools.permutations(currency_pairs, 3):
c1, c2 = triplet[0].split('/')
if (c1 in triplet[1] or c1 in triplet[2]) and (c2 in triplet[1] or c2 in triplet[2]):
set_triplets.add(tuple(sorted(triplet)))
for triplet in set_triplets:
print(triplet)
</code></pre>
<h3>输出:</h3>
^{pr2}$
<p><em>请注意,三元组中货币对的顺序是按字典顺序升序排列的,不要期望第一对货币对总是连接其他两个货币对。</em></p>