我有一个字符串,其名称用“and”分隔,以两种形式输入:“First Last”或“Last, First”。我想生成一个仅包含按字母顺序排序的姓氏的列表。我是 Python 新手,所以我想知道是否有更短或更好的方法来做我所做的事情:
names = 'John Foo and Baz, Mike Tom and Bar Foo, S. P.'
authors = [ i.strip() for i in names.split("and") ]
comma = [ i.split(',')[0] for i in [i for i in authors if "," in i] ]
nocomma = [ i.split()[-1] for i in [i for i in authors if not "," in i] ]
surnames = comma + nocomma
surnames.sort()
print surnames