[英]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.
代码不断给我带来错误,无法解包的值太多,可能是因为例如此名称ELSWOCK Rick Jr具有名字中间名和姓氏。 Here Rick Jr should be the first name and Elswock as last name.
在这里,Rick Jr应该是名字,Elswock应该是姓氏。
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()最后一个文件“”,第10行,第一=第[0] [:]。split()行
ValueError: too many values to unpack (expected 2)
ValueError:太多值无法解包(预期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: 编辑以符合OP的格式要求:
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']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.