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]?