2016-11-09 19 views
0

itertoolsをPython 2.7で使用すると、コードのどの部分(リンクで強調表示されている部分)を繰り返す必要があるのでしょうか?また、このケースではイテレータまたはコンビネーションイテレータの方が優れていますか?私はIDの数(numID)とArcGIS(arcpyを使用)のフィールドを参照して、新しいタイプのIDを作成しています。目的はコードを短縮し、すべてのnumIDとrow [x] = "...-(01)"をリストすることです。これは私が書いてイテレータしようとしていたため、コード、次のとおりです。Python itertools IDフィールドの数または組み合わせ

if numIDs == "1": 
     row[3] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (01)" 
     cursor.updateRow(row) 
    elif numIDs == "2": 
     row[3] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (01)" 
     row[4] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (02)" 
     cursor.updateRow(row) 
    elif numIDs == "3": 
     row[3] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (01)" 
     row[4] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (02)" 
     row[5] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (03)" 
     cursor.updateRow(row) 
    elif numIDs == "4": 
     row[3] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (01)" 
     row[4] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (02)" 
     row[5] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (03)" 
     row[6] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (04)" 
     cursor.updateRow(row) 
    elif numIDs == "5": 
     row[3] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (01)" 
     row[4] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (02)" 
     row[5] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (03)" 
     row[6] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (04)" 
     row[7] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (05)" 
     cursor.updateRow(row) 
    elif numIDs == "6": 
     row[3] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (01)" 
     row[4] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (02)" 
     row[5] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (03)" 
     row[6] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (04)" 
     row[7] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (05)" 
     row[8] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (06)" 
     cursor.updateRow(row) 
    elif numIDs == "7": 
     row[3] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (01)" 
     row[4] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (02)" 
     row[5] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (03)" 
     row[6] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (04)" 
     row[7] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (05)" 
     row[8] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (06)" 
     row[9] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (07)" 
     cursor.updateRow(row) 
    elif numIDs == "8": 
     row[3] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (01)" 
     row[4] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (02)" 
     row[5] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (03)" 
     row[6] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (04)" 
     row[7] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (05)" 
     row[8] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (06)" 
     row[9] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (07)" 
     row[10] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (08)" 
     cursor.updateRow(row) 
    elif numIDs == "9": 
     row[3] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (01)" 
     row[4] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (02)" 
     row[5] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (03)" 
     row[6] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (04)" 
     row[7] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (05)" 
     row[8] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (06)" 
     row[9] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (07)" 
     row[10] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (08)" 
     row[11] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (09)" 
     cursor.updateRow(row) 
    elif numIDs == "10": 
     row[3] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (01)" 
     row[4] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (02)" 
     row[5] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (03)" 
     row[6] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (04)" 
     row[7] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (05)" 
     row[8] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (06)" 
     row[9] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (07)" 
     row[10] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (08)" 
     row[11] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (09)" 
     row[12] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (10)" 
     cursor.updateRow(row) 
+0

'' list'または 'dict'オブジェクトをrow'ていますか? –

+0

次のご質問については、[mcve]をお読みください。 – boardrider

答えて

0

私はあなたが何をしようとしてわからないんだけど、そのコードは私が茂みの下に非表示にすることができます。

これはあなたのコードとまったく同じことを行います。

for i in range(numIDs): 
    row[i+3] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (%02i)" %(i+1) 
    cursor.updateRow(row) 
0

このコードは同等です:

for i in range(1, numIDs+1): 
    row[i+2] = "({}) - ({}) - ({}) - (%02i)".format(row[0], row[1], row[2], i) 
    cursor.updateRow(row) 
関連する問題