如何使列表中元素的第一个结果等于0,然后使用此结果计算下一个结果?

2024-09-25 00:36:19 发布

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

def formula(listA, count_zero=0, start=0, result=[], interarrival=1):
    for i in listA:
        if i == 1:
            service_time=3
            result.append(max(start + service_time - (count_zero + 1) * interarrival, 0))
            count_zero = 0
        elif i == 0:
            count_zero += 1
    return result


print(formula([1, 1, 1, 0, 1, 0, 0, 1]))

当我运行这个程序时,得到的结果#[2, 2, 2, 1, 0],这不是我期望的结果。你知道吗

'''
For the elements in listA = [1, 1, 1, 0, 1, 0, 0, 1]

For the 1st element:
i == 1, because this is the first element, the program is supposed to compute 0 which is why I set the start = 0

For the 2nd element: 
i == 1, the formula computes:
max(start + service_time - (count_zero + 1) * interarrival, 0)
max(0 #start from 1st element + 3 #service_time - (count_zero + 1) #no zeros right before second element * interarrival, 0)
max(0 + 3 - (0 + 1) * 1, 0)
max(3 - (1)*1, 0) 
max(3 - 1, 0)
max(2, 0) #the max of these two numbers is 2 is the programs is supposed to print 2


For the 3rd element: 
i == 1, so the formula computes:
max(start + service_time - (count_zero + 1) * interarrival, 0)
max(2 #start from 2nd element + 3 #service_time - (count_zero + 1) #no zeros right before third element * interarrival, 0)
max(2 + 3 - (0 + 1)*1, 0)
max(5 - 1*1, 0) 
max(5 - 1, 0)
max(4, 0) #the max of these two numbers is 4, the program is supposed to print 4


For the 4th element:
i == 0, the program simply counts this zero and doesn't do anything else, nothing is printed as expected.


For the 5th element: 
i == 1, so the formula computes:
max(start + service_time - (count_zero + 1) * interarrival, 0)
max(4 #start from 2nd element + 3 #service_time - (count_zero + 1) #one zero right before fifth element * interarrival, 0)
max(4 + 3 - (1 + 1)*1, 0)
max(7 - 2*1, 0) 
max(7 - 2, 0)
max(5, 0) #the max of these two numbers is 4, the program is supposed to print 4


For the 6th element:
i == 0, the program simply counts this zero and doesn't do anything else, nothing is printed as expected. 


For the 7th element:
i == 0, the program also simply counts this zero and doesn't do anything else, nothing is printed as expected.


For the 8th element: 
i == 1, so the formula computes:
max(start + service_time - (count_zero + 1) * interarrival, 0)
max(5 #start from 5th element + 3 #service_time - (count_zero + 1) #two zeros right before third element * interarrival, 0)
max(5 + 3 - (2 + 1)*1, 0)
max(8 - 3*1, 0) 
max(5, 0)
max(5, 0) #the max of these two numbers is 5, the program is supposed to print 5

'''

我该如何纠正这个错误?基本上,当i == 1等于0时,我尝试为第一个元素生成第一个结果,然后使用这个结果作为start来计算下一个结果,然后使用下一个结果作为start来计算之后的结果。你知道吗

我已经尽可能清楚地解释了这个问题,如果不清楚,请一定要问。你知道吗


Tags: thefortimeiscountserviceelementprogram
1条回答
网友
1楼 · 发布于 2024-09-25 00:36:19

你有i==1,公式计算。你的逻辑都不知道列表的第一个元素。你知道吗

你可以试着列举,这就给出了位置。你知道吗

for i, x in enumerate(listA):
    if i == start:
        # at start of list 

    if x == 1:
        # compute formula 

顺便说一下,不要将result存储为函数的默认参数,将其设置为局部变量

而且start永远不会更新。总是零。。。你打算让这个函数递归吗?你知道吗

相关问题 更多 >