Check if substring is in a list of strings?

前端 未结 4 1857
灰色年华
灰色年华 2020-12-08 03:52

I have found some answers to this question before, but they seem to be obsolete for the current Python versions (or at least they don\'t work for me).

I want to chec

4条回答
  •  半阙折子戏
    2020-12-08 04:25

    You can import any from __builtin__ in case it was replaced by some other any:

    >>> from  __builtin__ import any as b_any
    >>> lst = ['yellow', 'orange', 'red']
    >>> word = "or"
    >>> b_any(word in x for x in lst)
    True
    

    Note that in Python 3 __builtin__ has been renamed to builtins.

提交回复
热议问题