Skip to content Skip to sidebar Skip to footer

Splitting List Of Names Where There Might Be Common Last Name For Two First Names

In Python, I'm parsing through a large list of names, something that looks like this: [u' Ron Iervolino, Trish Iervolino, Russ Middleton, and Lisa Middleton ', u' Barbara Loughlin

Solution 1:

As a more efficient way for your first match you can use str.split() (if your string has been split with , ):

>>> s=u' Ron Iervolino, Trish Iervolino, Russ Middleton, and Lisa Middleton '>>> [i.split('and')[1] if i.strip().startswith('and') else i for i in s.split(',')]
[u' Ron Iervolino', u' Trish Iervolino', u' Russ Middleton', u' Lisa Middleton ']

and for find the name in u' Kelly and Tom Murro ' you can use the following :

l=[]
s=u' Ron Iervolino, Trish Iervolino, Russ Middleton, and Lisa Middleton ,Kelly  and Tom Murro'import re
for i in s.split(','):
   i=i.strip()
   if i.startswith('and') :
      l.append(i.split('and')[1])
   elifnot i.endswith('and') and'and'in i :
      names=[i for i in re.split(r'and| ',i) if i]
      for t inzip(names[:-1],[names[-1] for i inrange(len(names)-1)]):
          l.append(' '.join(t))
   else: 
      l.append(i)

print l
[u'Ron Iervolino', u'Trish Iervolino', u'Russ Middleton', u' Lisa Middleton', u'Kelly  Murro', u'Tom  Murro']

When you encounter with strings like u' Kelly and Tom Murro ' first you split it to a list of names with [i for i in re.split(r'and| ',i) if i] that split the string based on 'and' , space so you will have [u'Kelly', u'Tom', u'Murro']. then as you want the following names :

u'Kelly  Murro'u'Tom  Murro'

you can create a zip file with repeat the last element and the named from begin of the list to last names[:-1] so you will have the following . note that this recipe work for longest names like (Kelly and Tom and rose and sarah Murro) :

[(u'Kelly', u'Murro'), (u'Tom', u'Murro')]

Solution 2:

this should give you an idea, use this pattern first

([A-Z]\w+\s+[A-Z]\w+)|([A-Z]\w+)(?=\s+and\s+[A-Z]\w+\s+([A-Z]\w+))

and replace w/ $1$2 $3Demo

Post a Comment for "Splitting List Of Names Where There Might Be Common Last Name For Two First Names"