String contains all the elements of a list

前端 未结 5 1005
暖寄归人
暖寄归人 2021-01-05 18:32

I am shifting to Python, and am still relatively new to the pythonic approach. I want to write a function that takes a string and a list and returns true if all the elements

5条回答
  •  庸人自扰
    2021-01-05 19:03

    For fun, I thought I'd do it with iterators and map:

    from operator import contains
    from itertools import imap, repeat
    
    def myfun(str, list):
        return all(imap(contains, repeat(str), list))
    

    Then I realised that this essentially does the same thing as the accepted answer, but probably with more method calls.

    def myfun(str, list):
        return all(letter in str for letter in list)
    

提交回复
热议问题