2016-07-19 14 views
1

dlist=[[1, 2], [3, 4], [5, 6], [7, 8],[9,10]]と言っています。そして、私はインデックスと一緒に2つのリストの組み合わせを一緒にしたい。Itertoolsとインデックスの組み合わせ

所望の出力は

[[0,1],[[1,2],[3,4]]], [[0,2],[[1,2],[5,6]]] and so on.. 

次のコードは、

[((0, [1, 2]), (1, [3, 4])), ((0, [1, 2]), (2, [5, 6])),.... 

clist.append([list(itertools.combinations(list(enumerate(dlist)), 2))])

代わりに...

+0

「0」はどこから来たのですか?あなたは各要素から減算していますか? – Will

+0

はdlistの[1,2]のインデックスです – revry

答えて

3

あなたはほとんどそこを作成しています。 clistを希望の形式に変換するだけです。 itertools.combinations(enumerate(dlist), 2)は、((index_number_of_sub_arr1, sub_arr1), (index_number_of_sub_arr2, sub_arr2))の形式の組み合わせの反復子を返します。 イテレータをウォークして、フォーマットを[[index_number_of_sub_arr1, index_number_of_sub_arr2],[sub_arr1, sub_arr2]]に変換するだけです。

# this is like your current clist (iterator) 
tmp_list = itertools.combinations(enumerate(dlist), 2) 

# convertion to desired format 
clist = [ [[idx1, idx2], [arr1,arr2]] for ((idx1, arr1),(idx2,arr2)) in tmp_list ] 
関連する問題