擅长:python、mysql、java
<p>以下是一种主要使用标准库功能的方法:</p>
<pre><code>from collections import Counter
from itertools import combinations, chain
raw_pairs = ['BCH/BTC', 'BCH/ETH', 'DASH/USD', 'BTC/USDT', 'ETH/BTC']
def parse_pair(raw_pair):
return raw_pair.split('/')
def is_trair(trair_candidate):
# assuming that all currencies appear twice means we have a trair
currency_counts = Counter(chain.from_iterable(trair_candidate))
return set(currency_counts.values()) == {2}
pairs = map(parse_pair, raw_pairs)
trair_candidates = combinations(pairs, r=3)
# filter the actual trairs from all trair_candidates
trairs = filter(is_trair, trair_candidates)
print(list(trairs))
</code></pre>
<p>将提供:</p>
<blockquote>
<p>[(['BCH', 'BTC'], ['BCH', 'ETH'], ['ETH', 'BTC'])]</p>
</blockquote>