Fastest way to check if a string contains specific characters in any of the items in a list

前端 未结 4 1084
隐瞒了意图╮
隐瞒了意图╮ 2021-01-02 04:33

What is the fastest way to check if a string contains some characters from any items of a list?

Currently, I\'m using this method:

lestring = \"Text1         


        
4条回答
  •  离开以前
    2021-01-02 05:01

    if the test is to see if there are any characters in common (not words or segments), create a set out of the letters in the list and then check the letters agains the string:

    char_list = set(''.join(list_of_words))
    test_set = set(string_to_teat)
    common_chars = char_list.intersection(test_set)
    

    However I'm assuming you're looking for as little as one character in common...

提交回复
热议问题