循环直到每个元素返回true

时间:2020-07-01 12:05:59

标签: python loops if-statement while-loop

我有一个带有ID(列表)的列表。使用wget进行在线检查的结果是,该列表的每个元素都返回字符串“ true”或“ false”。 我想遍历该列表,只要有一个元素返回“假”值即可。 基本上我想重复一遍:

for i in range(len(list)):
  wget online check
  if status == 'true':
    write id to another list
  elif status == 'false':
    continue
  time.sleep()

一遍又一遍,直到一切都变成真。

我尝试了一个嵌套的while循环:

for j in range(len(list)):
    while status_ == 'false':
        wget online check
      if status == 'true':
        write id to another list
      elif status == 'false':
        continue
      time.sleep()

但这不起作用。 有人可以帮忙吗?

欢呼

4 个答案:

答案 0 :(得分:6)

使用deque作为循环队列,在双端队列成功时将其从值中删除。只要deque不为空,循环就会继续。

(双端队列就像一个列表,但是您可以有效地向任一端添加元素或从其中删除元素。)

from collections import deque

d = deque(list)

while d:
    i = d.popleft()
    wget online check
    if status == "true":
        write id to another list
    else:
        d.append(i)  # Put it back to try again later
    time.sleep(...)

答案 1 :(得分:1)

您可以尝试存储每次遇到False时都设置为False的值。

flag = False

while not flag:
    flag = True 
    for i in range(len(list)):
      wget online check
      if status == 'true':
        write id to another list
      elif status == 'false':
        flag = False
        continue
      time.sleep()

答案 2 :(得分:0)

list是一个坏名字,我认为您是在all(..)any(..)之后:

k = [True, False, False, True]
print(k)
while any(not i for i in k):   # loop as long as one value is False
    for i, v in enumerate(k):
        if not v:              # for demo purposes: change 1 False to True
            k[i] = True        
            break
    print(k)

print("done")

输出:

[True, False, False, True]
[True, True, False, True]
[True, True, True, True]
done

文档:

答案 3 :(得分:0)

如果我正确理解了您的问题,请尝试在获得select coordinate, max(case when seqnum = 1 then name end) as name_1, max(case when seqnum = 1 then value end) as value_1, max(case when seqnum = 2 then name end) as name_2, max(case when seqnum = 2 then value end) as value_2 from (select t.*, row_number() over (partition by coordinate order by id) as seqnum from t ) t group by coordinate; 的代码块末尾放置一个break

希望对您有帮助,祝您有愉快的一天。

相关问题