简体   繁体   中英

I have a list of names and i am trying to extract first name and last name from the list in python

Below is the code I am working on, I am not able to extract the first names and last names from the names list. The code keeps giving me error too many values to unpack may be because for example this name ELSWOCK Rick Jr is having first middle and last name. Here Rick Jr should be the first name and Elswock as last name.

names=[' HE XF, Wei W, Liu ZZ, Shen XL',' STARK LE, AARON FIN, LEO DE CAP, ADAM FORTH, KARAN SINGH',' ELSWICK RICK Jr, ASTO FON, SAM MARLON, KIM ZENG']
names1 = []
for l1 in names:
    names1.append(l1.split(',')) #To split the line based on commas
first_names=[]
last_names=[]
for line in names1:
    last,first= line[0][:].split()
    first_names.append(first)
    last_names.append(last)

Results in this error:

Traceback (most recent call last):
File "", line 10, in last,first= line[0][:].split()

ValueError: too many values to unpack (expected 2)

The output I am expecting would be like this:

first_names=[ 'XF, W, ZZ, XL', 'LE, FIN, CAP, FORTH, SINGH', 'RICK Jr, FON, MARLON, ZENG' ]
last_names=[' HE, Wei, Liu, Shen',' STARK, AARON, LEO DE, ADAM, KARAN',' ELSWICK, ASTO, SAM, KIM']

Edited to fit OP's format requirements:

names=[' HE XF, Wei W, Liu ZZ, Shen XL',' STARK LE, AARON FIN, LEO DE CAP, ADAM FORTH, KARAN SINGH',' ELSWICK RICK Jr, ASTO FON, SAM MARLON, KIM ZENG']
names1 = []
for l1 in names:
    names1.append(l1.split(','))
first_names=[]
last_names=[]

for sub_list in names1:
  temp_sub_firsts ="" 
  temp_sub_lasts ="" 
  for full_name in sub_list:
    full_name_split = full_name.split(' ')
    full_name_split.pop(0)
    temp_sub_lasts += full_name_split.pop(0)
    if full_name != sub_list[-1]:
      temp_sub_lasts += ', '
    temp_first = ""
    for sub_first in full_name_split:
      temp_first += sub_first + ' '
    temp_sub_firsts += temp_first
    if full_name != sub_list[-1]:
      temp_sub_firsts += ', '
  first_names.append(temp_sub_firsts)
  last_names.append(temp_sub_lasts)
print(first_names)
print(last_names)

Outputs:

first_names[]=

['XF , W , ZZ , XL ', 'LE , FIN , DE CAP , FORTH , SINGH ', 'RICK Jr , FON , MARLON , ZENG ']

last_names[]=

['HE, Wei, Liu, Shen', 'STARK, AARON, LEO, ADAM, KARAN', 'ELSWICK, ASTO, SAM, KIM']

You can try this too

names=[' HE XF, Wei W, Liu ZZ, Shen XL',' STARK LE, AARON FIN, LEO DE CAP, ADAM FORTH, KARAN SINGH',' ELSWICK RICK Jr, ASTO FON, SAM MARLON, KIM ZENG']
reg1=re.compile(r"\w+(?<!,)\s(?=(?!Jr)[\w ]+,?)")
reg2=re.compile(r'(?<!,)\s(?:(?!Jr|DE)[\w ]+(?=,?))')
first_names=[reg1.sub("",m.strip()) for m in names]
last_names=[reg2.sub("",m.strip()) for m in names]
print("{}\n{}".format(first_names,last_names))

Output is

['XF, W, ZZ, XL', 'LE, FIN, CAP, FORTH, SINGH', 'RICK Jr, FON, MARLON, ZENG']
['HE, Wei, Liu, Shen', 'STARK, AARON, LEO DE, ADAM, KARAN', 'ELSWICK, ASTO, SAM, KIM']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM