String contains all the elements of a list

前端 未结 5 902
暖寄归人
暖寄归人 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 18:50

    If you're not worried about repeat characters, then:

    def myfunc(string, seq):
        return set(seq).issubset(string)
    

    And, untested, if you do care about repeated characters, then maybe (untested):

    from collections import Counter
    def myfunc(string, seq):
        c1 = Counter(string)
        c2 = Counter(seq)
        return not (c2 - c1)
    

提交回复
热议问题