python 当我的列表中有多个项目时遇到编写循环逻辑的问题

python facing problems writing for loop logic when I have multiple items in my list

当我的列表中只有一项时,我的 for 循环工作。请参阅下面的代码:

remove_month_year = ["apple","banana"]   
for remove_m_y in remove_month_year:
    if remove_m_y not in time_posted: #I want to exceute this line of code if "apple" and "banna" not found in time_posted
       ....my others code

#time_posted is list of string like time_posted = "he eat apple" 

但是当我添加了不止一项时,我遇到了问题。让你解释一下: 假设我的列表中有两个项目 ["apple","banana"] 所以当我 运行 开始 for 循环时它会检查 "apple" 如果没有找到苹果然后它会 运行 我的其他代码块没关系。当它 检查 banana 并发现 apple 和我的其他块代码 运行ning 时出现问题,如果找到 apple,我将逻辑不添加到 运行 代码。

它一次用一个变量遍历数组,所以我想你可以添加一个布尔值,该值在一个出现时改变,然后在循环后有一个条件来检查该布尔值,或者更多有效的方法是


remove_month_year = ["apple","banana"]   
if(not any(remove_m_y in time_posted for remove_m_y in remove_month_year)):
       ....my others code

#time_posted is list of string like time_posted = "he eat apple" 

不确定这是否有效,但我认为应该……

任何 returns 如果任何元素为真则为真,如果所有元素为真则为假,所以这可能是完美的