どのように私はPythonで長さnのリストのすべての可能な組み合わせを得ることができますが、私は文字列のコードを書いたが、上記のコードpythonで長さnのリストのすべての可能な組み合わせitertoolsなし
def combo(w, l):
lst = []
for i in range(len(w)):
if l == 1:
lst.append(w[i])
for c in combo(w[i+1:], l-1):
lst.append(w[i] + c)
return lst
comb=map(list,combo('12345',3))
文字列の組み合わせを得ることです、それは正しい出力を与える:
['12', '13', '14', '15', '23', '24', '25', '34', '35', '45']
itertools.combinations(['1'、 '2'、 '3'、 '4'、 '')のリスト要素 'print '[" "。join(x) ''、 '13'、 '14'、 '15'、 '23'、 '24'、 '25'、 '34'、 ' 35 '、' 45 ']' – Manjunath
@Manjunathが追加されました。 – RoadRunner
これを追加していただきありがとうございます。 – Manjunath