Python itertools Count или Combination for ID Fields

Используя itertools в Python 2.7, как указать, какие части кода (выделенные в ссылке) должны быть повторены? Кроме того, итератор count или итератор combination лучше для этого случая? Я перечисляю номера ID (нумиды) и ссылаюсь на поля в ArcGIS (используя arcpy) для создания нового типа ID. Цель состоит в том, чтобы сократить код и теперь перечислить все нумиды и строки[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 ответа

  1. Я не уверен, что вы пытаетесь сделать, но этот код заставляет меня хотеть спрятаться под кустом.

    Это делает точно так же, как ваш код:

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