Foreach循环仅在包含namedtuple的列表上循环一次

时间:2020-08-14 11:48:37

标签: python for-loop namedtuple

我有这段代码 从集合中导入namedtuple

Device = namedtuple('Device', ['Name', 'Timestamp'])

#add samples
deviceList = [Device(Name = "alo", Timestamp="00000"), Device(Name = "lilo", Timestamp="1111"), Device(Name = "piko", Timestamp="2222")]

def findDeviceByName(listOfDevices, DeviceName):
    print(listOfDevices)
    for x in listOfDevices:
        if (DeviceName == x.Name):
            return True
        else: 
            return False

print(findDeviceByName(deviceList, "lilo")) #returnes False even if lilo is in deviceList
print(len(deviceList)) #prints 3

出于某种原因,此foreach循环在列表的第一个元素处停止,但是您可以清楚地看到列表中有3个元素,即使len()如此表示

不确定为什么会这样,如果有人有任何想法,请告诉我 感谢您的问候和问候!

0 个答案:

没有答案