Python itertools ID字段的计数或组合

时间:2016-11-09 17:21:21

标签: python python-2.7 iterator itertools

在Python 2.7中使用itertools,我如何表示代码的哪些部分(链接中突出显示)应该重复?此外,对于这种情况,计数迭代器还是组合迭代器更好?我列出了ArcGIS中的ID(numID)和引用字段的数量(使用arcpy)来创建新类型的ID。目标是缩短代码,现在列出每个numID和row [x] =" ...-(01)"。这是我试图编写的代码和迭代器:

    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)

2 个答案:

答案 0 :(得分:0)

我不确定你要做什么,但那段代码让我想藏在灌木丛中。

这与您的代码完全相同:

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

答案 1 :(得分:0)

此代码相当于:

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