无法找到元组值

问题描述 投票:0回答:2

我建立重叠的日期时间的元组。 start_overlap和end_overlap是datetime对象。

在每个“在time_tuple项目”循环,我想看看如果在该项目中存在的时间。这种比较总是失败,甚至当被评价的项目确实存在的时间。

time = start_overlap
end = end_overlap
time_tuple = (...contains a bunch of items in the format of (str(time),1), ...)

while time < end:    
    if time_tuple == ():            
        time_tuple +=(str(time),1), 

    else:
        for item in time_tuple:

            if str(time) in str(item): #this never resolves to true
                print('time found in item')    
                pass

            else:
                time_tuple +=(str(time),1),

    time = time + timedelta(minutes=7.5)

你能看到我的检查,看看是否STR(时间)是该项目有什么错我的条件语句?

谢谢

python
2个回答
0
投票

指令str(time) in str(item)比较的项,其是datetime的字符串表示一个typing.Tuple[str, int]的字符串表示。这将不可避免地失败。

我无法测试它,因为我没有一个完全工作的基本代码,但它应该与str(time) in item替换上述指令工作,或者,如果你愿意,str(time) == item[0]

希望这可以帮助。


-1
投票

改变这一行:

if str(time) in str(item):

为此:

if (str(time),1) === str(item):
© www.soinside.com 2019 - 2024. All rights reserved.