gpt4 book ai didi

python-3.x - 当Python中的嵌套列表内满足值时,如何结束While循环?

转载 作者:行者123 更新时间:2023-12-02 19:25:23 24 4
gpt4 key购买 nike

我有一个嵌套列表,当通过 For-Loop 搜索嵌套循环内的值时,我试图结束 while 循环。我的问题是它永远循环。

listA = [
['a','b'], # 0, -5
['b','c'], # 1, -4
['c','d'], # 2, -3
['a','d'], # 3, -2
['b','e'], # 4, -1
]

endpoint = 'c'
point = ''
while point is not endpoint:
for sets in listA:
for point in sets:
print(point)

我希望输出是这样的:

a
b
b
c

最佳答案

你可以这样做:-

listA = [
['a','b'], # 0, -5
['b','c'], # 1, -4
['c','d'], # 2, -3
['a','d'], # 3, -2
['b','e'], # 4, -1
]

endpoint = 'c'
done = False
for sets in listA:
if done:
break
for point in sets:
print(point)
if point == endpoint:
done=True
break

关于python-3.x - 当Python中的嵌套列表内满足值时,如何结束While循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62488186/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com