简体   繁体   中英

Adding more than one parameter of search in a loop

I want to add more than one parameter to search in a list of sublist. For example, this is when i am looking just for one specific word.

y2 = [x for x in y2 if 'Entity' in x]

but im looking for a group of words and i just try to put the list in the code, but doesnt work. The error that appears is this.

'in ' requires string as left operand, not list

entities = [["Entity","Entity with some","Entity with audition"]]
y2 = [x for x in y2 if entities  in x]

Thank you for the help.

You could make your own function and use it in the list comprehension.

def f(x, entities):
    for e in entities:
        if e in x:
            return True
    return False

y2 = [x for x in y2 if f(x, entities)]

You can use any .

entities = ["Entity","Entity with some","Entity with audition"]
y2 = [x 
      for x in y2 
      if any(ent in x for ent in entities)
     ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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