0

I have a list of strings and I want to sort the resulting list by string length. Strings with equal length should be sorted by how often the letter ’A’ occurs in them, so that the ones with the largest number of As come first and the others next. I have tried using lambda function to sort but its not working

result = sorted(result, key=len)
or
result.sort(lambda x,y: cmp(len(x), len(y)))

This is perfectly sorting based on the length of the string, but I have another condition to sort on number of As if the length is equal, how can I achieve this?

I have tried the following and yet was not able to figure it out

  result.sort(key = (lambda x,y: 1 if len(x)>len(y) else (-1 if len(x)<len(y) else (1 if x.count("A")>y.count("A") else -1))))


 result = sorted(result, lambda x,y: 1 if len(x)>len(y) else (-1 if len(x)<len(y) else (1 if x.count("A")>y.count("A") else -1)))

I have tried both sort and sorted and I always get an error sort() takes no positional arguments if I don't specify key= Infront of lambda and sorted expected 1 arguments, got 2

6
  • python sorting is stable, so when you need to sort by A then by B then by C, you can always do x.sort(C); x.sort(B); x.sort(A). No need to shoehorn everything in one single lambda. Commented Oct 27, 2022 at 8:27
  • its not actually sort on a single character, the condition is to sort on length then if the length of both the strings are equal then the string having more As should come first Commented Oct 27, 2022 at 8:37
  • in the above comment, A, B, C are conditions (lambdas), not characters Commented Oct 27, 2022 at 8:41
  • "lambda x,y: cmp(len(x), len(y))" python removed comparator functions in P3, current python uses a key function, you just return what to compare the objects by rather than perform the result of the comparison yourself. You should stop using Python 2 documentation. Commented Oct 27, 2022 at 8:45
  • Incidentally "it's not working" is about as unhelpful as can be, readers figure out things are not working because you're asking a function. How is it not working is a lot more useful (error messages, expected v observed results, ...) Commented Oct 27, 2022 at 8:46

1 Answer 1

3

You can try

a = ["AAd", "aAAd", "AAAd", "adAA"]
a.sort(key=lambda x: (len(x), -x.count('A')))
# output
[ 'AAd', 'AAAd', 'aAAd', 'adAA']

We are firstly sorting based on the length if length is equal then sort based on -x.count('A') so if we have 3 elements then we do counting of A in them, lets say counting is [3, 2, 1] so if we arrange them in ascending order we will get [1,2,3] but we need [3,2,1] so we appended negative sign in them like this [-3, -2, -1].

Sign up to request clarification or add additional context in comments.

6 Comments

Sorry If I was not clear, I only want to sort based on count if the length of both the string are equal so in your example I want [ 'AAd', 'AAAd', 'aAAd', 'adAA']
Got your point, I updated the answer now check
Thank you very much, it worked. I might be asking a simple question but could you please explain what does -x.count('A') means here, I know the count function for a string but I couldn't figure out why adding -x is working.
Let me add the explanation in the answer.
ahh got it, another stupid question, but how is it picking up the count condition only if the length of the strings are same? we didn't mention it anywhere right, I am in the assumption that it will check both the conditions on every iteration.
|

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.