I have two lists:
main_list = ['Smith', 'Smith', 'Roger', 'Roger-Smith', '42']
master_list = ['Smith', 'Roger']
I want to count the number of times when I find the line from master_list in the main_list line, not counting the same element twice.
Example: for these two lists, the result of my function should be 4. "Smith" can be obtained 3 times in the main list. “Roger can be found 2 times, but since Smith has already been found in Roger Smith, this one is no longer taken into account, so Roger just counts 1, which is 4 in total.
The function I wrote for review is below, but I think there is a faster way to do this:
def string_detection(master_list, main_list):
count = 0
for substring in master_list:
temp = list(main_list)
for string in temp:
if substring in string:
main_list.remove(string)
count+=1
return count
source
share