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

后端 未结 6 1130
小鲜肉
小鲜肉 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:55

    You can do it other way around. Create list that will contain only elements from main_list that have substring from master_list

    temp_list = [ string for string in main_list if any(substring in string for substring in master_list)]
    

    Now temp_list looks like this:

    ['Smith', 'Smith', 'Roger', 'Roger-Smith']
    

    So the length of temp_list is your answer.

提交回复
热议问题