如何排序此列表?蟒蛇

时间:2015-11-28 09:37:07

标签: python list sorting

mylist = [
    'Attendance w1-2 (Responses) - Form Responses 1.csv',
    'Attendance w15-2 (Responses) - Form responses 1.csv',
    'Attendance w5-1 (Responses) - Form Responses 1.csv',
    'Attendance w7-2 (Responses) - Form Responses 1.csv',
    'Attendance w13-2 (Responses) - Form responses 1.csv',
    'Attendance w5-2 (Responses) - Form Responses 1.csv',
    'Attendance w9-2 (Responses) - Form Responses 1.csv',
    'Attendance w4-2 (Responses) - Form Responses 1.csv',
    'Attendance w1-1 (Responses) - Form Responses 1.csv',
    'Attendance w12-2 (Responses) - Form responses 1.csv',
    'Attendance w2-1 (Responses) - Form Responses 1.csv',
    'Attendance w14-2 (Responses) - Form responses 1.csv',
    'Attendance w3-1 (Responses) - Form Responses 1.csv',
    'Attendance w12-1 (Responses) - Form responses 1.csv',
    'Attendance w14-1 (Responses) - Form responses 1.csv',
    'Attendance w8-2 (Responses) - Form Responses 1.csv',
    'Attendance w4-1 (Responses) - Form Responses 1.csv',
    'Attendance w15-1 (Responses) - Form responses 1.csv',
    'Attendance w3-2 (Responses) - Form Responses 1.csv',
    'Attendance w6-2 (Responses) - Form Responses 1.csv',
    'Attendance w7-1 (Responses) - Form Responses 1.csv',
    'Attendance w2-2 (Responses) - Form Responses 1.csv',
    'Attendance w6-1 (Responses) - Form Responses 1.csv',
    'Attendance w11-1 (Responses) - Form Responses 1.csv'
]

如何根据wx-x对此列表进行排序?

1 个答案:

答案 0 :(得分:1)

输出需要:

['Attendance w1-1 (Responses) - Form Responses 1.csv',
 'Attendance w1-2 (Responses) - Form Responses 1.csv',
 'Attendance w2-1 (Responses) - Form Responses 1.csv',
 'Attendance w2-2 (Responses) - Form Responses 1.csv',
 'Attendance w3-1 (Responses) - Form Responses 1.csv',
 'Attendance w3-2 (Responses) - Form Responses 1.csv',
 'Attendance w4-1 (Responses) - Form Responses 1.csv',
 'Attendance w4-2 (Responses) - Form Responses 1.csv',
 'Attendance w5-1 (Responses) - Form Responses 1.csv',
 'Attendance w5-2 (Responses) - Form Responses 1.csv',
 'Attendance w6-1 (Responses) - Form Responses 1.csv',
 'Attendance w6-2 (Responses) - Form Responses 1.csv',
 'Attendance w7-1 (Responses) - Form Responses 1.csv',
 'Attendance w7-2 (Responses) - Form Responses 1.csv',
 'Attendance w8-2 (Responses) - Form Responses 1.csv',
 'Attendance w9-2 (Responses) - Form Responses 1.csv',
 'Attendance w11-1 (Responses) - Form Responses 1.csv',
 'Attendance w12-1 (Responses) - Form responses 1.csv',
 'Attendance w12-2 (Responses) - Form responses 1.csv',
 'Attendance w13-2 (Responses) - Form responses 1.csv',
 'Attendance w14-1 (Responses) - Form responses 1.csv',
 'Attendance w14-2 (Responses) - Form responses 1.csv',
 'Attendance w15-1 (Responses) - Form responses 1.csv',
 'Attendance w15-2 (Responses) - Form responses 1.csv']

如果是这样的话:

import re
mylist.sort(key=lambda x: float('.'.join(re.findall(' w(\d+)\-(\d+) ', x)[0]
)))