在 Python 中编辑列表 [重复]

问题描述 投票:0回答:1

我有一个全名列表,其中 Forenames 和 Surnames 用逗号分隔,例如:

Authors = ['Shakespeare, William', 'Dafoe, Daniel', 'Pilcher, Rosamunde']

我需要一个只包含姓氏而不包含名字的新列表:

AuthorsSurname = ['Shakespeare', 'Dafoe', 'Pilcher']

我怎样才能到达那里?我试着用

搜索作者列表
        regexAuthors = re.compile(r',$')
        AuthorsSurname = (regexAuthors.findall(Authors))

匹配逗号之前的所有条目并创建一个新列表,但它说我不能在这里使用“Authors”作为参数,因为它不是字符串。

(链接主题没有帮助)

python list edit matching
1个回答
1
投票
Authors = ['Shakespeare, William', 'Dafoe, Daniel', 'Pilcher, Rosamunde']

surname = [val.split(",")[0] for val in Authors]
# ['Shakespeare', 'Dafoe', 'Pilcher']
© www.soinside.com 2019 - 2024. All rights reserved.