id = 77bb1b03
id_list = ['List of devices attached ', '572ea01e\tdevice', '77bb1b03\tdevice', '']
if id in id_list:
print "id present"
else:
print "id not present"
>>> from __future__ import print_function
... id = '77bb1b03'
... id_list = ['List of devices attached ', '572ea01e\tdevice', '77bb1b03\tdevice', '']
... [print(item, 'ID present') for item in id_list if item and item.find(id)!=-1]
77bb1b03 device ID present
id = '77bb1b03'
id_list = ['List of devices attached ', '572ea01e\tdevice', '77bb1b03\tdevice', '']
if any(i.startswith(id) for i in id_list):
print "id present"
else:
print "id not present"
看看这个:
你需要的是一行代码,id是一个字符串!你知道吗
感谢@Achampion的左-右执行!你知道吗
首先,修复代码:
然后在列表上循环并分别比较每个字符串:
假设您只想匹配起始值:
注意:假设
id
实际上是一个字符串,因为它不是有效的文本。你知道吗相关问题 更多 >
编程相关推荐