博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python sort list customisation
阅读量:4029 次
发布时间:2019-05-24

本文共 11238 字,大约阅读时间需要 37 分钟。

Sorting a Python List the Simple Way

Okay, so if you only want to sort a list of numbers, Python has a built in function that does all the hard work for you.

Say we have a list of numbers:

And we want to sort them in ascending order. All we do is call sort on the list, for in-place sorting, or the built in function sorted for not modifying the original list and returning a new sorted list. Both functions take in the same arguments and can be treated as "the same" for our purposes here, except for the reason above.

Here we go:

What about descending order?

Here you go:

What is Python doing behind the scenes? It's calling a version of mergesort on the list. It calls the function __cmp__ on each object when comparing values, and decides which one to put in front of the other based on the value returned from __cmp__. The return value is 0 for equal to, 1 for greater than, and -1 for less than the compared value. We'll use this information later to make our own objects sortable.

What about tuples, you say? I'm getting to that.

Sorting a Python Tuple the Simple Way

Since tuples are arrays that you cannot modify, they don't have an in-place sort function that can be called directly on them. They must always use the sorted function to return a sorted list. Keeping that in mind, here's how to do it:

Notice how sorted returns an array.

Okay, now let's see how to sort something a little more complicated.

Sorting a List of Lists or Tuples

This is a little more complicated, but still pretty easy, so don't fret! Both the sorted function and the sort function take in a keyword argument called key.

What key does is it provides a way to specify a function that returns what you would like your items sorted by. The function gets an "invisible" argument passed to it that represents an item in the list, and returns a value that you would like to be the item's "key" for sorting.

Let me illustrate, for your superb eyes only, the key keyword argument!

So, taking a new list, let's test it out by sorting by the first item in each sub-list:

Here we can see that the list is now sorted by the first item in the sub-list in ascending order. Note that you could have used the sort function as well, but I personally like the sorted function better, so I'll be using that in further examples.

What happened? Remember that "invisible" argument I was talking about? That's what gets passed into the getKeyfunction every time the sorted needs a value. Tricky, tricky python ;).

Sorting by the second item in each sub-list would be as simple as changing the getKey function to this:

Alright. All good. Now, what about a list of tuples? Glad you asked!

It's actually the exact same as our example above, but has the list defined as such:

The only thing that has changed is that we now get a list of tuples instead of a list of lists returned to us.

The exact same solution can be applied to a tuple of tuples, so I'm not going to go there, as that would just be redundant. It would also waste more of those digital trees that make this beautiful digital paper.

Sorting a List (or Tuple) of Custom Python Objects

Here's a custom object that I created:

Let's make a list of them, for sorting purposes:

Okay, so we've got all of our fancy new custom objects in a list and we want to be able to sort them. How do we do that?

Well, we could define a function, like we did above, that takes in the item and returns a list. So let's do that.

This one is a little different, because our object is not a list anymore. This allows us to sort by the number property in the custom object.

So if we run the sorted function on our list of fancy custom objects, we get this:

A big mess of ugliness that we don't understand. Perfect. But don't worry, dear viewer, there is a simple fix we can apply to our custom object to make it all better!

Let's redefine our object class like this:

Ok, what the heck did we just do? First, the __repr__ function tells Python how we want the object to be represented as. In more complex terms, it tells the interpreter how to display the object when it is printed to the screen.

So, we tell Python to represent the object by it's class name, name, and number.

Now lets try sorting it again:

That's much better! Now we can actually tell that it sorted properly!

But there's a little bit of a problem. It's just nit-picking, but I don't want to have to type that key keyword every time I call sorted.

So, how do I do that? Well, remember that __cmp__ function I told you about? Let's put it into action!

Let's redefine our object one more time like this:

Looking good. What this does is tell Python to compare the value of the current object to another object in the list to see how it compares. Like I said above, the sorted function will call the __cmp__ function on the objects it is sorting in order to determine where they should be in relation to other objects.

Now we can just call sorted without worrying about including the key keyword, like so:

And voilà! It works nicely. Please note that all of the above also applies to tuples of the custom object. But I like to save my digital trees, as you know.

Sorting a Heterogeneous List of Custom Python Objects

Alright. Since Python is a dynamic language, it doesn't so much care about what objects we throw into lists. They can all be the same type, or they can all be different.

So let's define another different object to use with our Custom object.

It's a similar object, but still different from our Custom object.

Let's make a list of these and our Custom objects:

Now if we try to run sorted on this list:

We get a lovely error. Why? Because Custom doesn't have an attribute called age and AnotherObject doesn't have an attribute called number.

What do we do? Panic!

Just kidding. We know what to do. Let's redefine those objects again!

Awesome! What did we just do? We defined a common getKey function that both of the objects have so that we can compare them easily.

So now if we run the sorted function again, we get:

Nice! Now our objects can be compared and sorted to their hearts content.

You still prefer the using the key keyword, you say?

You can do that too. If you leave out the __cmp__ functions in each object, and define an outside function like so:

And then call sorted like so:

And there you have it! Pretty straight forward, but not as straight forward as some might guess. Still, Python makes it pretty easy with its built in sorted function.

For more sorting ideas, head over to . You could also check out  for instructions on how to sort with lambda functions.

And there you are. One step closer to becoming the world's Python master.

转载地址:http://johbi.baihongyu.com/

你可能感兴趣的文章
No.148 - LeetCode771
查看>>
No.174 - LeetCode1305 - 合并两个搜索树
查看>>
No.175 - LeetCode1306
查看>>
No.176 - LeetCode1309
查看>>
No.182 - LeetCode1325 - C指针的魅力
查看>>
mac:移动python包路径
查看>>
mysql:sql create database新建utf8mb4 数据库
查看>>
mysql:sql alter database修改数据库字符集
查看>>
mysql:sql alter table 修改列属性的字符集
查看>>
mysql:sql drop table (删除表)
查看>>
mysql:sql truncate (清除表数据)
查看>>
scrapy:xpath string(.)非常注意问题
查看>>
yuv to rgb 转换失败呀。天呀。谁来帮帮我呀。
查看>>
yuv420 format
查看>>
YUV420只绘制Y通道
查看>>
yuv420 还原为RGB图像
查看>>
LED恒流驱动芯片
查看>>
驱动TFT要SDRAM做为显示缓存
查看>>
使用file查看可执行文件的平台性,x86 or arm ?
查看>>
qt5 everywhere 编译summary
查看>>