1

Ok, I apologize in advance - I am new to python and I am sure that there are similar questions, but frankly the hardest part of learning python thus far has been wrapping my head around the syntax and usage of sorting with lambda.

I have a list of strings, each of these strings begins with a newline and a number. Like:

27 Tacos Cheese Mango Habanero Salsa
42 Burritos Cheese Beans Beef
7 Chocolates Cherry Strawberry Vanilla

I want to sort this list based on the number that is in the beginning of the string. From my (very limited) understanding of lambda and the examples in the documentation found here: https://wiki.python.org/moin/HowTo/Sorting , I feel like this should work:

sort = sorted(string.split('\n'), key=string.split()[0])
for i in sort: print i

So my question is twofold:

What am I doing wrong?

and

Could I just as easily sort this list of strings by the 5th word; ie string.split()[4]?

0

2 Answers 2

2

You are close. key has to specify a function that takes one argument. You, however, are trying to use a list element as an argument. Try this instead:

>>> string = '''27 Tacos Cheese Mango Habanero Salsa
349 Tacos Cheese Mango Habanero Salsa
1 Tacos Cheese Mango Habanero Salsa'''
>>> sorted(string.split('\n'), key=lambda x: int(x.split()[0]))  # the function is the lambda expression
['1 Tacos Cheese Mango Habanero Salsa', '27 Tacos Cheese Mango Habanero Salsa', '349 Tacos Cheese Mango Habanero Salsa']

The lambda expression is an anonymous function that is the same as the following function:

def name(x):
    return int(x.split()[0])

But you don't have to declare the function before using it as the key for the sorted function.

And yes, you can also do the same thing with the fifth item of the list, as long as there is a fifth item:

>>> sorted(string.split('\n'), key=lambda x: x.split()[5])
['27 Tacos Cheese Mango Habanero Salsa', '349 Tacos Cheese Mango Habanero Salsa', '1 Tacos Cheese Mango Habanero Salsa']
Sign up to request clarification or add additional context in comments.

5 Comments

It hope that int is not required. i worked out and get the answer, but you posted the same. +1
@suhail Thanks for the feedback. I think the OP wants to sort the list based on the number at the beginning of the line. If we don't convert it to an int, then the sort will place 200 before 21, since the two numbers will be compared as strings rather than ints.
Ok - I think I understand a lot more about this now. The arbitrary "x" in the lambda part has always confused me - I didn't know what "x" was, and I think I have a better idea now. This isn't working for my code, however, and I know why - the strings I'm manipulating have bash color codes in them at the beginning, and that's wreaking havoc with both the split() methods and the sorting itself. I'm going to have to figure out how to work around it when I have more time. In any case - thank you for replying!
@Locane Great! I'm glad to hear you are understanding it better.
This solution works in the test environment I posed, thank you again!
0

Functions that are not bound to a name are Lambda functions. For eg:

g = lambda x: x+2 is same as: def f (x): return x+2 ie:

>>> f(8)
10
>>> g(8)
10

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.