递归传递三元运算符返回方法不支持项目分配

时间:2019-07-09 04:59:12

标签: python-3.x recursion ternary-operator arcpy

我有下表。我需要代码来递归计算最小开始日期,并找到此日期和finishdate之间的Date范围。如果范围<= 60,则Cat应该增加1,否则应保持为-1。挑战在于,def函数中的三元运算符拒绝更新,因为我的方法未在第62行分配项。

OBJECTID_1 *    Conc *  ID  start   finish  DateRange   Cat
1   ASPELBJNMI  LAPMT   6/27/2018   8/11/2018   100 -1
2   ASPELBJNMI  KLKIY   8/22/2018   8/31/2018   100 -1
3   ASPELBJNMI  HDOKK   8/12/2018   9/30/2018   100 -1
4   ASPELBJNMI  XMCZW   8/22/2018   10/20/2018  100 -1
5   BKRKAVQTZC  FCBES   7/1/2018    8/15/2018   100 -1
6   BKRKAVQTZC  NHQGW   8/24/2018   9/4/2018    100 -1
7   BKRKAVQTZC  LVUSS   8/9/2018    10/4/2018   100 -1
8   BKRKAVQTZC  RQVNN   8/18/2018   10/24/2018  100 -1
9   FKKTHEJGCY  IHORM   7/2/2018    8/20/2018   100 -1
10  FKKTHEJGCY  AZFEV   8/28/2018   9/8/2018    100 -1
11  FKKTHEJGCY  YAVOY   8/4/2018    9/29/2018   100 -1
12  FKKTHEJGCY  AMWAX   8/16/2018   10/22/2018  100 -1
13  JUNRNEXCRG  QCOWV   6/30/2018   8/25/2018   100 -1
14  JUNRNEXCRG  AOYNL   8/25/2018   9/9/2018    100 -1
15  JUNRNEXCRG  CGCHK   8/4/2018    10/3/2018   100 -1
16  JUNRNEXCRG  IQYGJ   8/12/2018   10/26/2018  100 -1

我尝试了各种方法来更新行。

我尝试使用dict理解并调用dict值。 我还尝试让def函数以递归方式合并生成器,列表和元组以及调用值。结果是一样的。

https://community.esri.com/message/861460-update-cursor-not-honoring-selection-in-a-for-loop

我的代码

import arcpy
from datetime import datetime
from datetime import datetime as dt
from datetime import timedelta
import time
from datetime import date
#Variables Start
arcpy.env.workspace = r'C:datap\New File Geodatabase.gdb'
fc= 'Classifyjoin212'
field =['Conc','ID']
fields = ['start', 'finish', 'DateRange', 'Cat']
Classifyjoin = "Classifyjoin"
Classifyjoin__2_ = Classifyjoin

#Def function

def min_cat(b=datetime.now() , m=datetime.now(), x = 2, y=-1):
"""This function calculates the date range between earliest start in a category to the exact finish date of an individual member"""""
#return(b-m).days
     lg =[2]
     e = (b-m).days
     f = {True: x, False: y} [e <= 60]#(y, x) [e <=60 ] # [ x if e<=60 else -1]
     return [ e, f]
#print min_cat(b=dt.strptime('2019-06-26', '%Y-%m-%d').date(),  m=dt.strptime('2018-11-29', '%Y-%m-%d').date(), x= 9, y=-1)

temp_lyr= arcpy.MakeTableView_management(fc, "temp_lyr")
ud = {}

with arcpy.da.SearchCursor(fc, field) as rows:
    for row in rows:
        if row[0] not in ud:
            ud[row[0]] = row[1]          
for k in ud:             
    where_clause="Conc IN( '%s')" % k #+ "AND ID IN( '%s')" %v
    #print(where_clause)
    Sele = arcpy.SelectLayerByAttribute_management(temp_lyr, 'NEW_SELECTION', where_clause)#Carry out the selection
    cnt = arcpy.GetCount_management(temp_lyr) # Counting the selected and copied features
    print("The number of selected records is: " + str(cnt))   
    listdates = []
    listCat =[]
    bv =[]
    #k = [row[3] for row in arcpy.da.SearchCursor(fc, ["start", "finish", "DateRange", "Cat"]) if row [2]<60]
    with arcpy.da.UpdateCursor(fc, fields) as cursor:

        for row in cursor:
            listCat.append(row[3])
            jp =max(listCat)
            kp = int('{}'.format(jp))
            kp +=1
                #while row[3]==0:
            if row[3]==-1:
                listdates.append(row[0])

                pg =min(listdates)#.date()
                jup = '{}'.format(pg)
                jop = dt.strptime(jup, '%Y-%m-%d %H:%M:%S').date()

                bg = '{}'.format(row[1])
                job = dt.strptime(bg, '%Y-%m-%d %H:%M:%S').date()
                a =min_cat(b= job, m=jop, x= kp, y=-1)
                row[2] = a[0]
                cursor.updateRow(row)
                rows[3] = a[1]
                cursor.updateRow(rows)               
    del cursor, row    

所需结果:

OBJECTID_1 *    Conc *  ID  start   finish  DateRange   Cat
1   ASPELBJNMI  LAPMT   6/27/2018   8/11/2018   45  0
3   ASPELBJNMI  HDOKK   8/12/2018   9/30/2018   49  1
2   ASPELBJNMI  KLKIY   8/22/2018   8/31/2018   19  1
4   ASPELBJNMI  XMCZW   8/22/2018   10/20/2018  59  2
5   BKRKAVQTZC  FCBES   7/1/2018    8/15/2018   45  0
7   BKRKAVQTZC  LVUSS   8/9/2018    10/4/2018   56  1
8   BKRKAVQTZC  RQVNN   8/18/2018   10/24/2018  67  -1
6   BKRKAVQTZC  NHQGW   8/24/2018   9/4/2018    26  1
9   FKKTHEJGCY  IHORM   7/2/2018    8/20/2018   49  0
11  FKKTHEJGCY  YAVOY   8/4/2018    9/29/2018   56  1
12  FKKTHEJGCY  AMWAX   8/16/2018   10/22/2018  67  -1
10  FKKTHEJGCY  AZFEV   8/28/2018   9/8/2018    35  1
13  JUNRNEXCRG  QCOWV   6/30/2018   8/25/2018   56  0
15  JUNRNEXCRG  CGCHK   8/4/2018    10/3/2018   60  1
16  JUNRNEXCRG  IQYGJ   8/12/2018   10/26/2018  75  -1
14  JUNRNEXCRG  AOYNL   8/25/2018   9/9/2018    36  1

0 个答案:

没有答案