python - Foreach 循环仅在包含命名元组的列表上循环一次
问题描述
我有来自集合导入命名元组的这段代码
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() 也这么说
不知道为什么会这样
解决方案
推荐阅读
- testing - 测试链接 | 看不到任何创建新项目的选项 |
- linux - 在 Cron 上运行 Bash 脚本:Centos7
- grafana-loki - 如何使用 promtail 将 kubernetes 标签添加到日志数据中?
- tortoisesvn - TortoiseSVN 已修改文件的删除状态
- sql-server - 在选择查询 SQL Server 中选择行号
- android - MSAL acquireTokenSilent 挂起且永不返回
- php - 在 php 会话中管理用户组
- php - 如何在 laravel 中将文件写入桌面?
- android - 代码在模拟器中运行良好,但在真实设备中出错?
- c - 为什么当我尝试在 for 循环中使用 strcat() 时程序崩溃?