如何从给定的元组列表中提取和比较数据?
How to extract and compare data from a given list of tuples?
代码:
import datetime
data = [('09', '55', None, 'AC is on', None), ('10', '00',None, 'AC is on', None),('10', '13','fan is on', 'AC is on', 'light is on')]
def calc(data):
print(data) #prints the whole table
while True:
h=datetime.datetime.today().strftime("%H")
mi=datetime.datetime.today().strftime("%M")
# z=[(i[2],i[3],i[4]) for i in data if i[0] == h and i[1]==mi]
for i in data:
if i[0] == h and i[1]==mi:
print (i[2],i[3],i[4])
# sleep(60)
break
if __name__ == '__main__':
calc(data)
'data'中的前2个元素是用户输入的小时和分钟。该代码应采用用户输入的值,即此处的数据。并且应该检查当前时间并且应该打印 i[2],i[3],i[4],如代码所示。
1)我只想打印一次这些值。但是,它会持续检查并打印整整 1 分钟的值。 break 语句不起作用。
2)此外,是否可以以某种方式检查而不打印 none?
我对代码做了一些改动。
我不知道你为什么要使用 While= True 。因此它不会退出循环。所以我删除了它
刚刚在for check中添加了else语句,你可以去掉它
import datetime
data = [('09', '55', None, 'AC is on', None), ('10', '00',None, 'AC is on', None),('10', '13','fan is on', 'AC is on', 'light is on')]
def calc(data):
print(data) #prints the whole table
# while True:
h=datetime.datetime.today().strftime("%H")
print(h)
mi=datetime.datetime.today().strftime("%M")
print(mi)
# z=[(i[2],i[3],i[4]) for i in data if i[0] == h and i[1]==mi]
for i in data:
if i[0] == h and i[1]==mi:
print (i[2],i[3],i[4])
else:
print ("It does NOT match")
# sleep(60)
break
if __name__ == '__main__':
calc(data)
编辑(现在有效):
import datetime
data = [('09', '55', None, 'AC is on', None), ('10', '00',None, 'AC is on', None),('10', '52','fan is on', 'AC is on', None)]
def calc(data):
print(data) #prints the whole table
while True:
h=datetime.datetime.today().strftime("%H")
mi=datetime.datetime.today().strftime("%M")
# z=[(i[2],i[3],i[4]) for i in data if i[0] == h and i[1]==mi]
for i in data:
if i[0] == h and i[1]==mi:
print (i[2],i[3],i[4])
# sleep(60)
exit()
if __name__ == '__main__':
calc(data)
如果我理解正确的话,您希望您的程序 运行 直到找到当前时间与 [=12= 中的一个元组 (i
) 中指定的时间之间的匹配项]:(data[i][0], data[i][1])
,然后打印元组的其余部分:i[2:5]
break
在 for
循环范围内,而不是您的 while
循环。你退出 for
循环,是的,不是你的无限 while True
循环。
- 我宁愿做
while match_not_found:
,然后设置 match_not_found = True
,并在你的 break
语句之前有一行 match_not_found = False
。
- 如果您要使用元组的所有剩余项,则可以使用列表切片。
- 我添加了一个额外的
if
以排除 None
的打印项目
像那样:
import datetime
data = [('09', '55', None, 'AC is on', None), ('10', '00',None, 'AC is on', None),('10', '13','fan is on', 'AC is on', 'light is on')]
def calc(data):
print(data) #prints the whole table
match_not_found = True
while match_not_found:
h=datetime.datetime.today().strftime("%H")
mi=datetime.datetime.today().strftime("%M")
# z=[(i[2],i[3],i[4]) for i in data if i[0] == h and i[1]==mi]
for i in data:
if i[0] == h and i[1]==mi:
print ([j for j in i[2:5] if j != None])
match_not_found = False
break
if __name__ == '__main__':
calc(data)
代码:
import datetime
data = [('09', '55', None, 'AC is on', None), ('10', '00',None, 'AC is on', None),('10', '13','fan is on', 'AC is on', 'light is on')]
def calc(data):
print(data) #prints the whole table
while True:
h=datetime.datetime.today().strftime("%H")
mi=datetime.datetime.today().strftime("%M")
# z=[(i[2],i[3],i[4]) for i in data if i[0] == h and i[1]==mi]
for i in data:
if i[0] == h and i[1]==mi:
print (i[2],i[3],i[4])
# sleep(60)
break
if __name__ == '__main__':
calc(data)
'data'中的前2个元素是用户输入的小时和分钟。该代码应采用用户输入的值,即此处的数据。并且应该检查当前时间并且应该打印 i[2],i[3],i[4],如代码所示。
1)我只想打印一次这些值。但是,它会持续检查并打印整整 1 分钟的值。 break 语句不起作用。
2)此外,是否可以以某种方式检查而不打印 none?
我对代码做了一些改动。
我不知道你为什么要使用 While= True 。因此它不会退出循环。所以我删除了它
刚刚在for check中添加了else语句,你可以去掉它
import datetime
data = [('09', '55', None, 'AC is on', None), ('10', '00',None, 'AC is on', None),('10', '13','fan is on', 'AC is on', 'light is on')]
def calc(data):
print(data) #prints the whole table
# while True:
h=datetime.datetime.today().strftime("%H")
print(h)
mi=datetime.datetime.today().strftime("%M")
print(mi)
# z=[(i[2],i[3],i[4]) for i in data if i[0] == h and i[1]==mi]
for i in data:
if i[0] == h and i[1]==mi:
print (i[2],i[3],i[4])
else:
print ("It does NOT match")
# sleep(60)
break
if __name__ == '__main__':
calc(data)
编辑(现在有效):
import datetime
data = [('09', '55', None, 'AC is on', None), ('10', '00',None, 'AC is on', None),('10', '52','fan is on', 'AC is on', None)]
def calc(data):
print(data) #prints the whole table
while True:
h=datetime.datetime.today().strftime("%H")
mi=datetime.datetime.today().strftime("%M")
# z=[(i[2],i[3],i[4]) for i in data if i[0] == h and i[1]==mi]
for i in data:
if i[0] == h and i[1]==mi:
print (i[2],i[3],i[4])
# sleep(60)
exit()
if __name__ == '__main__':
calc(data)
如果我理解正确的话,您希望您的程序 运行 直到找到当前时间与 [=12= 中的一个元组 (i
) 中指定的时间之间的匹配项]:(data[i][0], data[i][1])
,然后打印元组的其余部分:i[2:5]
break
在for
循环范围内,而不是您的while
循环。你退出for
循环,是的,不是你的无限while True
循环。- 我宁愿做
while match_not_found:
,然后设置match_not_found = True
,并在你的break
语句之前有一行match_not_found = False
。 - 如果您要使用元组的所有剩余项,则可以使用列表切片。
- 我添加了一个额外的
if
以排除None
的打印项目
像那样:
import datetime
data = [('09', '55', None, 'AC is on', None), ('10', '00',None, 'AC is on', None),('10', '13','fan is on', 'AC is on', 'light is on')]
def calc(data):
print(data) #prints the whole table
match_not_found = True
while match_not_found:
h=datetime.datetime.today().strftime("%H")
mi=datetime.datetime.today().strftime("%M")
# z=[(i[2],i[3],i[4]) for i in data if i[0] == h and i[1]==mi]
for i in data:
if i[0] == h and i[1]==mi:
print ([j for j in i[2:5] if j != None])
match_not_found = False
break
if __name__ == '__main__':
calc(data)