![](/img/trans.png)
[英]How to check if any item in list starts with string and if so how to get item index?
[英]If any item of list starts with string?
我正在尝试检查列表中的任何项目是否以某个字符串开头。 我怎么能用for循环呢? IE:
anyStartsWith = False
for item in myList:
if item.startsWith('qwerty'):
anyStartsWith = True
使用any()
:
any(item.startswith('qwerty') for item in myList)
如果你想用for循环来做
anyStartsWith = False
for item in myList:
if item[0:5]=='qwerty':
anyStartsWith = True
0:5取字符串中的前6个字符,您可以根据需要调整它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.