我有一组名字,姓氏是大写的,中间名和中间名是正常的,例如。
OBAMA Barack
DEL MONTE Alfredo
我想在
中拆分它们"OBAMA", "Barack"
"DEL MONTE", "Alfredo"
达到此目的的pythonic方式是什么?
答案 0 :(得分:8)
>>> import itertools
>>> [
... ' '.join(items)
... for _, items in itertools.groupby('DEL MONTE Alfredo'.split(), str.isupper)
... ]
['DEL MONTE', 'Alfredo']
答案 1 :(得分:2)
def split_names(names):
for s in names:
last_names = []
name_parts = s.split()
while name_parts and name_parts[0].isupper():
last_names.append(name_parts.pop(0))
yield ' '.join(last_names), ' '.join(name_parts)
names = ["OBAMA Barack", "DEL MONTE Alfredo"]
for last_name, first_name in split_names(names):
print last_name
print first_name
print
打印:
OBAMA
Barack
DEL MONTE
Alfredo
答案 2 :(得分:2)
您可以使用简单的正则表达式:
import re
a = "DEL MONTE Alfredo"
first, last = re.match(r'([A-Z ]+)\s+(.+)', a).groups()
或循环通过单词列表并过滤掉全大写的单词:
first = ' '.join(w for w in a.split() if w.isupper())
last = ' '.join(w for w in a.split() if not w.isupper())
在我个人看来,“最pythonic”===“最简单”。
答案 3 :(得分:0)
试试这个:
(?![A-Z][a-z])([A-Z ]+) ([A-Z][a-z]+)