在python中比较字符串列表,但相等的值不出现为true

2024-10-04 09:26:04 发布

您现在位置:Python中文网/ 问答频道 /正文

在python2.7中比较列表中的字符串时遇到了一个问题。通过将鼠标悬停在VS中的变量上方,我可以看到字符串是相同的,但是程序不会输入这段代码的最后一个if:

result = [] # datetime object list
resultS = [] # dates converted in strings list
tipo = [] # verifying boolean list
oid = [] # object ids list
resultF = []
oidF = []
verifTCRA = False
# Keep the most recent dates and the type of data (if is TCRA or not)
with da.SearchCursor(fc, (campos), expressao) as sCursor2:
    for row in sCursor2:
        row0temp = row[num_campo1]
        row1temp = row[num_campo2]
        row2temp = row[num_campo3]
        if row0temp == None:
            row0temp = dataInicDt
        if row1temp == None:
            row1temp = dataInicDt
        if row2temp == None:
            row2temp = dataInicDt
        if ((row0temp > row1temp) and (row0temp > row2temp)):
            resultS.append(str(row[num_campo1]))
            result.append(row[num_campo1])
            tipo.append(False)
            oid.append(row[num_campo4])
        elif ((row1temp > row0temp) and (row1temp > row2temp)):
            resultS.append(str(row[num_campo2]))
            result.append(row[num_campo2])
            tipo.append(False)
            oid.append(row[num_campo4])
        elif ((row2temp > row0temp) and (row2temp > row1temp)):
            resultS.append(str(row[num_campo3]))
            result.append(row[num_campo3])
            tipo.append(True)
            oid.append(row[num_campo4])

MaxDat1 = max(result) # Biggest date of an AIA/NIS without TCRA
# If there is TCRA data, keeps in another list
for i in range(cont1):
    if tipo[i]:
        resultF.append(result[i])
        oidF.append(oid[i])
        verifTCRA = True
if verifTCRA:
    MaxData2 = max(resultF) # TCRA with biggest data in AIA/NIS
contInt = 0
MaxDat1S = str(MaxDat1)
# Keeps the object id of the data with the biggest date (tcra has priority)
if verifTCRA:
    for row in resultF:
        if row[0] == MaxData2:
            regCorr = oidF[contInt]
        contInt += 1
else:
    for row in resultS:
        #row0dt = dt.datetime.strptime(row[0], "%Y-%m-%d %H:%M:%S")
        if (row[0] == MaxDat1S):
            regCorr = oid[contInt]
        contInt += 1

日期信息以两种形式(datetime和string)附加到列表中,因为我无法在列表中检索要比较的日期,必须从datetime列表中查找最长日期


Tags: theinifresultresultsnumlistrow
1条回答
网友
1楼 · 发布于 2024-10-04 09:26:04

使用list.append(x)时要小心,因为在Python中列表是由引用表示的。 它可能在循环中每次都附加相同的精确列表。 试着先创建一个你想要附加的列表的深度副本,这将在内存中创建一个完全分离的对象,链接到该列表中

相关问题 更多 >