How to make it shorter (Pythonic)?

前端 未结 6 802
半阙折子戏
半阙折子戏 2021-02-15 16:56

I have to check a lot of worlds if they are in string... code looks like:

if \"string_1\" in var_string or \"string_2\" in var_string or \"string_3\" in var_stri         


        
6条回答
  •  天涯浪人
    2021-02-15 17:39

    Have you looked at filter?

    filter( lambda x: x in var_string, ["myString", "nextString"])
    

    which then can be combined with map to get this

    map( doSomething(), filter(lambda x: x in var_string, ["myString", "nextString"] ) )
    

    EDIT:

    of course that doesn't do what you want. Go with the any solution. For some reason I thought you wanted it done every time instead of just once.

提交回复
热议问题