简体   繁体   English

我有一个名称列表,我正在尝试从python列表中提取名字和姓氏

[英]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.

相关问题 如何使用姓氏和名字对列表进行排序 - How do i sort a list with first and last name by the last names 如何检查名字和姓氏是否在 Python 的列表中? 可能吗? - How to check first name and last name is in a list of names in Python? Is it possible? 我有一个带有 id 和名称的 class 用户,我正在尝试先以相反的顺序排列名称的用户列表,然后按 id 升序排列 - I have a class user with an id and name and I am trying to order a list of users with the name in reverse order first then by id in ascending order 假设我在python中有两个列表,一个名字列表和一个姓氏列表 - Suppose i have two lists in python a list of first names and a list of last names 在 python 中的用户提供姓名列表后,按字母顺序显示名字和姓氏字母 - Displaying the first name alphabetically and last name alphabetically after being given a list of names from the user in python 尝试使用列表中的正则表达式仅提取姓氏 - Trying to extract ONLY last name using regex from list 我正在尝试将全名拆分为熊猫中的第一个中间名和姓氏,但我被困在替换 - i am trying to split a full name to first middle and last name in pandas but i am stuck at replace 我正在尝试从我的用户那里获取:first_name 和 last_name,并注意到我的 html 代码中正在发生变化 - I am trying to get from my User: first_name and last_name, and noting is hapening in my html code 我正在尝试从使用 [] 和 [] 的列表列表中提取特定值 - I am trying to extract specific values from a list of lists use [] and [] 如何在包含全名的列表中提取名称(名字)的第一部分并丢弃一部分名称 - how to extract first part of name(first name) in a list that contains full names and discard names with one part
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM