添加更多元素时,while循环不起作用

时间:2018-07-17 14:14:50

标签: python while-loop

我有一个while循环,该循环比较每个if-elif语句中两个不同列表中的元素,并在满足所有条件时附加索引。这段代码有效,但是当我再添加两个if-elif语句时,它不起作用,我不确定为什么。

此代码有效:

i = 0
while i < len(startDate):
    j = 0
    while j < len(stopDate) and i < len(startDate):
        if startDate[i] != stopDate[j]:
            j += 1
        elif startDate[i] == stopDate[j]:
            if stopTime[j] < startTime[i]:
                j += 1
            elif stopTime[j] > startTime[i]:
                if startBehavior[i] != stopBehavior[j]:
                    j += 1
                elif startBehavior[i] == stopBehavior[j]:
                    interval.append(i)
                    interval.append(j)
                    i += 1

此代码不:

    i = 0
while i < len(startDate):
    j = 0
    while j < len(stopDate) and i < len(startDate):
        if startDate[i] != stopDate[j]:
            j += 1
        elif startDate[i] == stopDate[j]:
            if stopTime[j] < startTime[i]:
                j += 1
            elif stopTime[j] > startTime[i]:
                if startBehavior[i] != stopBehavior[j]:
                    j += 1
                elif startBehavior[i] == stopBehavior[j]:
                    if startInitiator[i] != stopInitiator[j]:
                        j += 1
                    elif startInitiator[i] == stopInitiator[j]:
                        if startReceiver[i] != stopReceiver[j]:
                            j += 1
                        elif startReceiver[i] == stopReceiver[j]:
                            interval.append(i)
                            interval.append(j)
                            i += 1

2 个答案:

答案 0 :(得分:0)

我相信一种更简洁的代码编写方式是:

i = 0
while i < len(startDate) and j < len(stopDate):
    j = 0
    if startDate[i] == stopDate[j] and startBehavior[i] == stopBehavior[j] and \
       startInitiator[i] == stopInitiator[j] and startReceiver[i] == stopReceiver[j] and \
       stopTime[j] > startTime[i]:
           interval.append(i)
           interval.append(j)
               i += 1
    else:
        j+=1

告诉我们是否可行。

答案 1 :(得分:0)

以更“ pythonic”的方式重写它(即使数据重组可能会提高质量):

def criteria_respected(startDate, stopDate, startTime, stopTime,
                       startBehavior, stopBehavior, startInitiator,
                       stopInitiator, startReceiver, stopReceiver):
    if startDate != stopDate:
        return False
    if startTime >= stopTime:
        return False
    if startBehavior != stopBehavior:
        return False
    if startInitiator != stopInitiator:
        return False
    if startReceiver != stopReceiver:
        return False
    return True

interval = list()
for i in range(len(startDate)):
    for j in range(len(stopDate)):
        if criteria_respected(startDate[i], stopDate[j], startTime[i], stopTime[j], startBehavior[i], stopBehavior[j], startInitiator[i], stopInitiator[j], startReceiver[i], stopReceiver[j]):
            interval.append(i)
            interval.append(j)

没有您的输入,我无法对其进行测试。

问题可能是您仅在满足所有条件时才增加i,如果从未满足,则无限循环。如果他们见面,您就不会[i][j+1]

在第二种情况下,

也> =“,这可能使您在特定数据集上无限期循环。两个版本的数据集是否相同?

如果它不起作用,您能给我们想要的输入和输出吗?