Python while在一个范围内循环

2024-05-29 06:46:35 发布

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

我有一个列表,如下所示。我想迭代列表,对于索引1中的每个“是”列表项,我想在得到另一个“是”之前,用“否”的出现次数更新索引2,然后向其中添加1。我不知道如何在遇到另一个“是”时在一个范围内迭代一段时间以停止

我已经简化了下面的问题和答案中的数据,但是下面是我对对象使用的代码,以尝试和完成

问题

fullchart = [
    [1, 'yes', 0],
    [2, 'no', 0],
    [3, 'no', 0],
    [4, 'yes', 0],
    [5, 'no', 0],
    [6, 'no', 0],
    [7, 'yes', 0],
    [8, 'no', 0],
    [9, 'yes', 0]
]

预期产量

[
    [1, 'yes', 3],
    [2, 'no', 0],
    [3, 'no', 0],
    [4, 'yes', 3],
    [5, 'no', 0],
    [6, 'no', 0],
    [7, 'yes', 2],
    [8, 'no', 0],
    [9, 'yes', 1]
]

代码

end_page = 0

for i in range(len(fullchart)):
    end_page = 0
    #pprint.pprint(fullchart[i][1])
    if fullchart[i][1] == 'yes':
        for b in range(i+1, len(fullchart)):
            print(b)
            if fullchart[i+1][1] == 'no':
                end_page += 1
                print('printing end_page')
                print(end_page)
        fullchart[i][3] == end_page
    else:
        pass

Tags: no代码in列表forlenifpage
3条回答

我想你要找的是break。一旦您找到下一个“是”,那么您希望捕获该值并打破嵌套for循环


fullchart = [
[1, 'yes', 0],
[2, 'no', 0],
[3, 'no', 0],
[4, 'yes', 0],
[5, 'no', 0],
[6, 'no', 0],
[7, 'yes', 0],
[8, 'no', 0],
[9, 'yes', 0]
]
end_page = 0


for i in range(len(fullchart)):
    end_page = 0
    if fullchart[i][1] == 'yes':
        print(f'checking.. i={i}')
        for b in range(i+1, len(fullchart)):
            print(f'\tchecking.. b={b}')
            if fullchart[b][1] == 'no':
                end_page += 1
                # print('printing end_page')
                # print(end_page)
            else:
                fullchart[i][2] = end_page+1
                break

    else:
        pass
print(fullchart)

输出

checking.. i=0
        checking.. b=1
        checking.. b=2
        checking.. b=3
checking.. i=3
        checking.. b=4
        checking.. b=5
        checking.. b=6
checking.. i=6
        checking.. b=7
        checking.. b=8
checking.. i=8
[[1, 'yes', 3], [2, 'no', 0], [3, 'no', 0], [4, 'yes', 3], [5, 'no', 0], [6, 'no', 0], [7, 'yes', 2], [8, 'no', 0], [9, 'yes', 0]]

您可以使用“是”来跟踪最后一个索引,并在适当的位置更新列表

base_idx = 0
for i, (_, bool, _) in enumerate(L):
    if bool == 'yes':
        L[base_idx][2] = i - base_idx
        base_idx = i
L[base_idx][2] = i - base_idx + 1

print(*L, sep='\n')

输出

[1, 'yes', 3]
[2, 'no', 0]
[3, 'no', 0]
[4, 'yes', 3]
[5, 'no', 0]
[6, 'no', 0]
[7, 'yes', 2]
[8, 'no', 0]
[9, 'yes', 1]

与其使用while语句,不如使用for循环,从最后一个循环遍历到第一个循环。这样你就可以计算每一个否定,直到你得到肯定

此解决方案只执行一次循环。您不需要嵌套循环。这是昂贵的和糟糕的编码

下面是执行此操作的代码:

a = [
[1, 'yes', 0],
[2, 'no', 0],
[3, 'no', 0],
[4, 'yes', 0],
[5, 'no', 0],
[6, 'no', 0],
[7, 'yes', 0],
[8, 'no', 0],
[9, 'yes', 0]
]

x = len(a)
yes = 0
for i in range(-1,-x-1,-1):
    print (a[i][1])
    if a[i][1] == 'yes':
        a[i][2] = yes+1
        yes = 0
    elif a[i][1] == 'no':
        yes +=1
    print (a[i],yes)
print (a)

其输出将为:

[[1, 'yes', 3], [2, 'no', 0], [3, 'no', 0], [4, 'yes', 3], [5, 'no', 0], [6, 'no', 0], [7, 'yes', 2], [8, 'no', 0], [9, 'yes', 1]]

查看与所需结果相似的数据的格式化方式为:

[
 [1, 'yes', 3], 
 [2, 'no',  0], 
 [3, 'no',  0], 
 [4, 'yes', 3], 
 [5, 'no',  0], 
 [6, 'no',  0], 
 [7, 'yes', 2], 
 [8, 'no',  0], 
 [9, 'yes', 1]
]

相关问题 更多 >

    热门问题