简体   繁体   English

遍历多个列表?

[英]Looping through multiple lists?

In my program, I grab the current time. 在我的程序中,我掌握了当前时间。 I then want to check if the current time is greater than t1 and less than t2. 然后,我想检查当前时间是否大于t1且小于t2。 For example: 例如:

00,00 <= dt.time() <= 06,00
06,01 <= dt.time() <= 12,00
12,01 <= dt.time() <= 18,00 
etc...

What I am doing is checking which quandrant of the day the current time is in. 我正在做的是检查当前时间是哪一天的时间。

If one out of the 4 statements ends up being true, then my program will move on to another function. 如果4个语句中的一个最终为true,则我的程序将转到另一个函数。

Right now, my program is acting like all 4 statements are true and it is looping several times. 现在,我的程序的行为就像所有4条语句都为真,并且循环了几次。

When I print everything to see what it is doing, it looks like each of the 4 objects in t1 are only being compared to the last of the 4 objects in t2. 当我打印所有内容以查看其功能时,看起来好像只是将t1中的4个对象中的每一个与t2中4个对象中的最后一个进行了比较。

Is there a way to do this properly? 有没有办法正确地做到这一点?

Here is what I am working on... 这是我正在做的...

from datetime import datetime, date, time

p1 = 1
p2 = 2
p3 = 3
p4 = 4

what_number_should_be = 3

def cycle():
    dt = datetime.now()
    t1 = [time(00,00), time(06,01), time(12,01), time(18,01)]
    t2 = [time(06,00), time(12,00), time(18,00), time(23,59)]
    user_number = [p1, p2, p3, p4]

    # I think the next 3 lines are throwing me off
    for x in t1:
        for y in t2:
            for z in user_number:

                # check if current date is between times listed in t1 and t2
                if x <= dt.time() <= y:
                    print(x, dt.time(), y)
                    print(z)
                    # if above is true, then check
                    # if user_number does not equal 3
                    if what_number_should_be != z:
                        print(z, " did not equal ", what_number_should_be)
                        # should only end up here once

cycle()

If I'm understanding the question correctly, you can replace your 3 for loops with 如果我对问题的理解正确,则可以将3个for循环替换for

for x, y, z in zip(t1, t2, user_number): . for x, y, z in zip(t1, t2, user_number):

Zip gives you the corresponding elements from each list: zip Documentation Zip为您提供了每个列表中的相应元素: zip文档

You can zip t1, t2, and user_number array together, which would result in a list of tuples of 您可以将t1,t2和user_number数组压缩在一起,这将导致一个元组列表

[(time(00,00), time(06,00), p1), (time(06,01), time(12,00), p2), (time(12,01), time(18,00), p3), (time(18,01), time(23,59), p4)]

and then run the loop like this 然后像这样运行循环

for x in zip(t1, t2, user_number):
    if x[0] <= dt.time() <= x[1]:
        print(x[0], dt.time(), x[1])
        print(x[2])
        if what_number_should_be != x[2]:
            print(x[2], " did not equal ", what_number_should_be)

If I understand correctly, for a given time dt , you want to determine the 'quadrant' of the day in which it resides, which you have chosen to encode in the following way: 如果我正确理解,对于给定的时间dt ,您想要确定其所在日期的“象限”,您已选择以以下方式对其进行编码:

  • If dt falls between 00,00 and 06,00, then it is in quadrant 1 如果dt介于00,00和06,00之间,则它在象限1
  • If dt falls between 06,01 and 12,00, then it is in quadrant 2 如果dt介于06,01和12,00之间,则它位于第2象限
  • If dt falls between 12,01 and 18,00, then it is in quadrant 3 如果dt介于12,01和18,00之间,则它在第3象限中
  • If dt falls between 18,01 and 23,59, then it is in quadrant 4 如果dt介于18,01和23,59之间,则它位于第4象限

I also gather that you expect your for-loop to print only 3 because you happen to be running this program between 12,01 and 18,00 in real time. 我还收集到您希望您的for循环仅输出3因为您恰巧在12,01到18,00之间实时运行此程序。

If my understanding of the problem is correct, then your use of two arrays to accomplish this is clever, and we can use them to discover the solution. 如果我对问题的理解是正确的,则可以聪明地使用两个数组来完成此任务,我们可以使用它们来发现解决方案。 By observing that because there are exactly 4 quadrants that we want to check for dt 's inclusion in, we can deduce that only need to iterate exactly 4 times, once for each of these quadrants. 通过观察,因为我们要检查dt是否包含4个象限,所以我们可以推断出只需要重复4次,每个象限重复一次。

This can be written as such: 可以这样写:

dt = datetime.now()
t1 = [time(00,00), time(06,01), time(12,01), time(18,01)]
t2 = [time(06,00), time(12,00), time(18,00), time(23,59)]

for quadrant in range(4):
    if (t1[quadrant] <= dt and dt <= t2[quadrant]):
        print("The current time is in quadrant: {0}".format(quadrant))

I hope this helps somewhat! 我希望这会有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM