Python Count the number of substring in list from other string list without duplicates

后端 未结 6 1115
小鲜肉
小鲜肉 2021-01-13 01:03

I have two list:

main_list = [\'Smith\', \'Smith\', \'Roger\', \'Roger-Smith\', \'42\']
master_list = [\'Smith\', \'Roger\']

I want to coun

6条回答
  •  囚心锁ツ
    2021-01-13 01:59

    If your master_list is not expected to be huge, one way to do it is with regex:

    import re
    
    def string_detection(master_list, main_list):
        count = 0
        master = re.compile("|".join(master_list))
        for entry in main_list:
            if master.search(entry):
                count += 1
        return count
    

提交回复
热议问题