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

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

    A one liner

    >>>sum(any(m in L for m in master_list) for L in main_list)
    4
    

    Iterate over main_list and check if any of the values from master_list are in that string. This leaves you with a list of bool values. It will stop after it finds one and so adds only one to the count for each string. Conveniently sum counts all the Trues to give you the count.

提交回复
热议问题