2016-11-15 10 views
0

Pythonでは、出力配列にすべての可能な座標ペアが含まれるように、2軸のリストが与えられた座標配列を作成するにはどうすればよいですか?2つのリストをPythonで配列座標に変換するには?

ax1=[1,3,4] 
ax2=[a,b] 

""" 
Code that combines them 
""" 

Combined_ax1 = [1,3,4,1,3,4,1,3,4] 
Combined_ax2 = [a,a,a,b,b,b,c,c,c] 

これは、複数のforループを使用せずにcombine_ax1とcombined_ax2を関数に渡すために必要です。次のように

+0

使用 'itertools.product'は、このを見てみましょう –

+0

を必要とするものになるだろう:http://stackoverflow.com/questions/533905/get-the-cartesian-product- Pythonのリストのシリーズの –

+4

'Combined_ax2、Combined_ax1 = zip(* itertools.product(['a'、 'b'、 'c']、[1、3、4]))'あなたに '(1、3、4、1、3、4、1、3、4)'と '( 'a'、 'a'、 'b'、 'b'、 'b' 'c'、 'c'、 'c') ' – jonrsharpe

答えて

2

このコードは、あなたが

import itertools 

ax1=[1,3,4] 
ax2=['a','b'] 

Combined_ax1, Combined_ax2 = zip(*itertools.product(ax1, ax2)) 
-1

これは、リストの理解を使用して行うことができる。

cartesian_product = [(x, y) for x in ax1 for y in ax2] 

このコードサンプル座標のすべての可能なペアを含むタプルのリストを返します。

関連する問題