2013-10-21 19 views
3

我的电子邮件地址的格式如下列表:排序使用正则表达式的列表在Python

name###@email.com

但数量并不总是存在。例如:[email protected][email protected] [email protected]等。我想按这些数字排序这些名称,而没有数字的则先排列。我已经提出了一些可行的方法,但是对于Python来说是新手,我很好奇它是否有更好的方法。这里是我的解决方案:

import re 

def sortKey(name): 
    m = re.search(r'(\d+)@', name) 
    return int(m.expand(r'\1')) if m is not None else 0 

names = [ ... a list of emails ... ] 
for name in sorted(names, key = sortKey): 
    print name 

这是我的脚本,我曾经用“SORTKEY”唯一的一次,所以我宁愿它是一个lambda函数,但我不知道该怎么做。我知道这将工作:

for name in sorted(names, key = lambda n: int(re.search(r'(\d+)@', n).expand(r'\1')) if re.search(r'(\d+)@', n) is not None else 0): 
    print name 

但我不认为我应该需要调用re.search两次这样做。在Python中做这件事最优雅的方式是什么?

回答

5

更好地使用re.findall就好像没有找到数字,然后它返回一个空列表,它将在填充列表之前排序。用来排序的关键是找到(转换为整数)的任何数字,后面跟随字符串本身...

emails = '[email protected] [email protected] [email protected]'.split() 

import re 
print sorted(emails, key=lambda L: (map(int, re.findall('(\d+)@', L)), L)) 
# ['[email protected]', '[email protected]', '[email protected]'] 

而且使用john1不是输出是:['[email protected]', '[email protected]', '[email protected]']这表明,虽然乔之后字典顺序,数量已已经考虑到了先行john

有,如果你想保持现有的在一个班轮(但呸)使用re.search的方法有点hackish的方式:

getattr(re.search('(\d+)@', s), 'groups', lambda: ('0',))() 
+0

此代码在Python 3失败! –