繁体   English   中英

如果任何列表项以字符串开头?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM