String contains all the elements of a list

前端 未结 5 1019
暖寄归人
暖寄归人 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:49

    def myfun(str,list):
       for a in list:
          if not a in str:
             return False
       return True
    

    return true must be outside the for loop, not just after the if statement, otherwise it will return true just after the first letter has been checked. this solves your code's problem :)

提交回复
热议问题