0

I have a list like this:

[
    'C:\\Users\\Rash\\Downloads\\Programs\\a.txt',
    'C:\\Users\\Rash\\Downloads\\a.txt',
    'C:\\Users\\Rash\\a.txt',
    'C:\\Users\\ab.txt',
    'C:\\Users\\aa.txt'
]

and I want to sort it based on two conditions:

  1. Sort by no of "\" present in the string.
  2. By Alphabets.

My end result should be this:

[
    'C:\\Users\\aa.txt',
    'C:\\Users\\ab.txt',
    'C:\\Users\\Rash\\a.txt',
    'C:\\Users\\Rash\\Downloads\\a.txt',
    'C:\\Users\\Rash\\Downloads\\Programs\\a.txt'
]

I am just learning lambda function in python and wrote this code:

print(sorted(mylist, key=lambda x:x.count("\\")))

but this code only sorts by count of "\". It does not sort it by alphabets. The result is that I am seeing the key "'C:\Users\ab.txt'" before the key "'C:\Users\aa.txt'".

I could sort the list two times, but I want to do it in one line. What should I add in the lambda code ? Since I am new to this whole "lambda" thing, I cannot think of a way to do this. Thanks for replying!! :)

1
  • 2
    A lambda expression is an alternate syntax for a simple def statement. lambda <args>: <expr> is nearly the same as def (<args>): return <expr>. The two differences are 1) the expression can be use in other statements and 2) the resulting function object gets the generic name '<lambda>' and does not get bound to a namespace name. Commented Nov 12, 2014 at 23:01

1 Answer 1

3

Return a sequence from the key function that contains the items you want to sort by.

key=lambda x: (x.count('\\'), x.split('\\'))
Sign up to request clarification or add additional context in comments.

7 Comments

I am getting the right answer now, but I do not fully understand what happened...so is that by using the split function, you are sub-dividing the string into lists and then all those lists get sorted, then joined again to form the string, then the count function is done on the sorted list, and then the final answer is printed ?
Python compares sequences naturally, i.e. the first elements of all sequences are compared, followed by the second elements, etc. The sorted function passes each element of mylist to the function in turn and sorts based on the resultant tuples.
please dont be mad at me for saying that I still dont fully understand. I was trying to learn more on lambda right now. Here is what I know so far. you returned a tuple whose first element is count of "\\" and second element is a list which is split by "\\". now python will use this tuple to sort. So when count is NOT equal, then only it will compare the list. But how does it compare the list. Is this why you wrote that "python compares sequences naturally" i.e. to compare a list, python will compare each element of the list and thus "aa.txt" comes before "ab.txt" ?
Correct. And (1, ['foo']) comes before (2, ['foo', 'bar']).
To be fair, that's more key than lambda. lambda just creates anonymous functions.
|

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.