0
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'
]

How to sort this list based on wx-x?

3
  • 4
    By using key attribute and a suitable lambda function. Do not excpect anyone here to do the job for you if you do not show what you have tried. Commented Nov 28, 2015 at 9:38
  • 3
    Also, what would the expected output be, and why? Should the numbers be interpreted lexicographically or numerically? Commented Nov 28, 2015 at 9:45
  • Sorry. I got this list by reading all file in folder and I'm not good with lamdba Commented Nov 28, 2015 at 9:46

1 Answer 1

1

Do need the output be:

['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']

If so:

import re
mylist.sort(key=lambda x: float('.'.join(re.findall(' w(\d+)\-(\d+) ', x)[0]
)))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.