-3
我有一個字符串,名稱用單詞「and」分隔,並以「First Last」或「Last,First」兩種形式輸入。我想要生成一個只有姓氏按字母順序排列的列表。 我是新來的Python,所以我在想,如果有做我所做的事,更短的或更好的方式:從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
使用codereview.stackexchange.com –
你想做什麼!?什麼是數據的一般形式!! ??請澄清。 – IcyFlame