itertools.product関数が返す可能性のあるすべての組み合わせを計算せずに、このコードを改善する方法を教えてください。 効率的に行うための他の解決方法はありますか?最小限の組み合わせを見つける方法[Python> itertools> product]:コードレビュー
これは私が試したものです:
import itertools
mylist = [[1,2,3],[1,3,4],[1,2,3]]
k = [set(i) for i in list(itertools.product(*mylist))]
k = sorted(k)
D_all = list(k for k, _ in itertools.groupby(k))
D_all.sort(key=len)
# Finding and displaying the minimum order-split combination
l = len(D_all[0])
print("Minimum number of Distributor Combination found is: {}".format(l))
print("The Possible combinations of {} are: ".format(l))
D_best = []
c = 0
for n,i in enumerate(D_all):
if len(i)<=l:
c +=1
print("{}:{}".format(c,i))
D_best.append(i)
if len(i)>l+1: break
出力:
Minimum number of Distributor Combination found is: 1
The Possible combinations of 1 are:
1:{'1'}
2:{'3'}
これは明らかではありません。あなたが 'singletons '(長さ1のセットを意味すると仮定します)だけを望むなら、' .product'で気にするのはなぜですか?リストから個々の要素を取り出すことができます。 – DeepSpace
あなたが探しているのは私には明確ではありません。 –
@DeepSpace私が理解していることは、OPはすべてのシングルトンが収穫されてから停止するまで、彼が与えるすべての '.product'を得ることです。 –