I have an array in which I'm getting duplicating string that is matching within the other string how can I check and remove from others strings if have
This is the array that I'm getting and I want to remove the duplication string from the array.
"titles": [
"Financial Analyst",
"Analyst",
"Respiratory Therapist",
"Therapist",
"Healthcare Specialist",
"Specialist",
"Liaison",
"Business Development Analyst",
"Development Analyst",
"Sales Team",
"Data Analyst",
"Administrator",
"Auditor",
"Director",
"Director of Events",
"Controller"
]
let me define which I want.
I have two strings in the array
Financial Analyst
& Analyst
I want to remove the second one from the array because this string comes in the first string.
also like one more example Healthcare Specialist
& Specialist
the second one I want to remove.
Thanks in advance.
CodePudding user response:
As long as optimization isn't a problem, you can easily achieve this using a for-loop. Here is the code with comments explaining it:
titles = [
"Financial Analyst",
"Analyst",
"Respiratory Therapist",
"Therapist",
"Healthcare Specialist",
"Specialist",
"Liaison",
"Business Development Analyst",
"Development Analyst",
"Sales Team",
"Data Analyst",
"Administrator",
"Auditor",
"Director",
"Director of Events",
"Controller"
] # Create the example titles array
for i in range(len(titles)-1, -1, -1): # Loop through the titles from top to bottom
for j in range(i-1, -1, -1): # Loop through the titles up until i from top to bottom
if titles[i] in titles[j]:
titles.remove(titles[i])
elif titles[j] in titles[i]:
titles.remove(titles[j])
print(titles)
Note that you have two if statements so that the order of the array doesn't matter.
CodePudding user response:
A very similar approach with a list comprehension, removing members that are inside any other string in the titles
list.
titles = [
"Financial Analyst",
"Analyst",
"Respiratory Therapist",
"Therapist",
"Healthcare Specialist",
"Specialist",
"Liaison",
"Business Development Analyst",
"Development Analyst",
"Sales Team",
"Data Analyst",
"Administrator",
"Auditor",
"Director",
"Director of Events",
"Controller"
]
titles = [title for title in titles if not any([title != _title and title in _title for _title in titles])]
Result:
[
"Financial Analyst",
"Respiratory Therapist",
"Healthcare Specialist",
"Liaison",
"Business Development Analyst",
"Sales Team",
"Data Analyst",
"Administrator",
"Auditor",
"Director of Events",
"Controller"
]