簡體   English   中英

遍歷列表以在 if 語句中查找子字符串

[英]Iterating through list for finding a substring in an if statement

我有一個字符串列表:

relations = ['displays', '1000displays', 'chooses', '1011chooses', '1020displays', 'clicks', '1031clicks', 'add to', 'checks', '1040checks', 'inserts discount offer to', '1050inserts discount offer to', 'inserts']

和一個子串:

t_object = 'discount offer'

並且我想僅當relations列表中不存在字符串discount offer時才執行 if 塊中的代碼。

我有以下代碼,但由於discount offer存在於relations ,因此它不應進入 if 語句中的代碼塊,但確實如此。 為什么?

if len(t_object) > len(objects[sentence_number]) and (s for s in relations if t_object not in s):
     print('I am in but I shoudlt be.')

使用any()函數。

if len(t_object) > len(objects[sentence_number]) and not any(t_object in s for s in relations):

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM