Question

I need to sort a dictionary file before I format it. I used list.sort and it put it in ascii order (capital letters before lowercase). So I found this code online to do the sort. It works, but I don't fully understand how the lambda works with 2 variables and with cmp(). I am confused as to what cmp is comparing and which two variables lambda is using. Please explain how lambda works with cmp inside of the sort function.

f = open("swedish.txt", 'r')
f2 = open("swed.txt", 'w')
doc = f.read().split('\n')

doc.sort(lambda x, y: cmp(x.lower(), y.lower()))

for line in doc:
    f2.write(line + '\n')

f.close()
f2.close()
Was it helpful?

Solution

To sort a list [b, a, c] one have to compare a with b and a with c and so on. Nothing else doest this lambda, it compares two components of the list with each other. For your case, the key-argument is more suitable:

doc.sort(key=str.lower)

OTHER TIPS

@Danial has the better answer for lower case compares. But since you asked about the lambda, in python 2.x, you can pass in a comparison function that takes 2 variables and returns -1, 0, 1 depending on whether var 1 is less than, equal to or greater than var 2. This feature has been removed in python 3, so consider it deprecated.

A lambda is just a function that hasn't been assigned to a variable and it is commonly used in cases like this where a call requires a function but you don't want to litter your code with small function definitions. lambda x,y: says you are defining a function with variables x and y, and cmp(x.lower(), y.lower()) is the implementation. In this case, you lower case the strings and cmp returns -1, 0 or 1 like sort requires.

Since you just want to lower case things, the cmp solution is much slower than the key solution. With key=, you only lower case the keys once, With the cmp function, you lower case two keys for every single compare.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top