Python 通过all()判断列表(list)中所有元素是否都包含某个字符串(string)
生活随笔
收集整理的這篇文章主要介紹了
Python 通过all()判断列表(list)中所有元素是否都包含某个字符串(string)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、判斷列表(list)中,所有元素是否在集合(set)中
list_string = ['big', 'letters'] string_set = set(['hello', 'hi', 'big', 'cccc', 'letters', 'anotherword'])result = all([word in string_set for word in list_string]) #結果是True2、判斷列表中的每個字符串元素是否含另一個列表的所有字符串元素中
''' 遇到問題沒人解答?小編創建了一個Python學習交流QQ群:579817333 尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學習教程和PDF電子書! ''' list_string= ['big', 'letters'] list_text = ['hello letters', 'big hi letters', 'big superman letters'] result = all([word in text for word in list_string for text in list_text]) #結果是False,因為'big'不在'hello letters'中。3、如果要獲取符合條件字符串,可以用 filter
list_string= ['big', 'letters'] list_text = ['hello letters', 'big hi letters', 'big superman letters'] all_words = list(filter(lambda text: all([word in text for word in list_string]), list_text )) print(all_words) #['big hi letters', 'big superman letters']總結
以上是生活随笔為你收集整理的Python 通过all()判断列表(list)中所有元素是否都包含某个字符串(string)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python中修改列表元素的2种方法
- 下一篇: python中F/f表达式优于forma