這個問題在這里已經有了答案: 如何檢查字串是否是字串串列中專案的子字串? (18 個回答) 昨天關門。
我想檢查我的串列中是否存在“-”。我已經嘗試了下面的代碼,但它沒有作業。
if '-' in txts:
print('yes')
串列
['?????', '????', '???', '470315-9428744', '???? ???', '???????', '?????? ', '-20210908', '????? ?????']
uj5u.com熱心網友回復:
它不起作用,因為您的串列本身不包含'-'。它包含'-20210908',然后包含'-'。因此,您需要一一檢查元素:
for s in txts:
if '-' in s:
print('yes')
break
或者,或者:
if any('-' in s for s in txts):
print('yes')
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/466829.html
標籤:Python python-3.x 列表 python-2.7
下一篇:如何在機器人框架中呼叫類方法?
