I have a list names
that contains lists of strings.
names = ['Acquaintance Muller', 'Vice president Johnson Affiliate Peterson Acquaintance Dr. Rose', 'Vice president Dr. John Mister Schmid, PRT Miss Robertson, FDU']
I want to split each string using names_office
in a loop over names
names_office = ['Acquaintance Muller', 'Miss Robertson, FDU', 'Vice president Johnson', 'Affiliate Peterson', 'Acquaintance Dr. Rose', 'Vice president Dr. John', 'Mister Schmid, PRT']
desired_output
names = ['Acquaintance Muller', 'Vice president Johnson', 'Affiliate Peterson', 'Acquaintance Dr. Rose', 'Vice president Dr. John', 'Mister Schmid, PRT', 'Miss Robertson, FDU']
I need to iterate irrespective of order.
CodePudding user response:
import re
re.findall("|".join(names_office), ",".join(names))
['Acquaintance Muller',
'Vice president Johnson',
'Affiliate Peterson',
'Acquaintance Dr. Rose',
'Vice president Dr. John',
'Mister Schmid, PRT',
'Miss Robertson, FDU']
The above will throw away values not found in the names_office list.
To maintain everything do:
results = []
pat = f"\\b({'|'.join(names_office)})\\b "
for i in names:
results.extend(re.sub(pat, "\\1:",i).split(':'))
print(results)
CodePudding user response:
I don't fully understand what you want to split by, but the following will split by ,
and flatten the results into a single list. Presumably you can adapt this to your use case, although I would be curious to understand the exact requirements, as I just couldn't figure it out from your question
[part.strip() for name in names for part in name.split(',')]