功能请求:排序列表切片 [英] Feature request: sorting a list slice

查看:68
本文介绍了功能请求:排序列表切片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果list.sort()接受另外两个可选的

参数,启动和停止,这样你就可以对切片进行排序。用

换句话说,


x =范围(1000000)

x.sort(start = 3,stop = -1)


相当于


x [3:-1] =已排序(x [3:-1])


但效率更高,无需重复切片两次。如果添加功能的时间不会太晚,我很乐意在2.5中看到这个。

George

It would be useful if list.sort() accepted two more optional
parameters, start and stop, so that you can sort a slice in place. In
other words,

x = range(1000000)
x.sort(start=3, stop=-1)

would be equivalent to

x[3:-1] = sorted(x[3:-1])

but more efficient and without repeating the slice twice. If it''s not
too late for feature additions, I''d love to see this in 2.5.
George

推荐答案

用例?

Use case?


George Sakkis写道:
George Sakkis wrote:
如果list.sort()接受了两个可选的
参数
It would be useful if list.sort() accepted two more optional
parameters




对什么有用的话会很有用?什么是用例?


< / F>



useful for what? what''s the use case ?

</F>


George Sakkis写道:
George Sakkis wrote:
如果list.sort()接受另外两个可选的
参数,启动和停止,这样你就可以对切片进行排序。换句话说,

x =范围(1000000)
x.sort(start = 3,stop = -1)

等同于

x [3:-1] =排序(x [3:-1])

但效率更高,无需重复切片两次。如果添加功能不太晚,我很乐意在2.5中看到这一点。
It would be useful if list.sort() accepted two more optional
parameters, start and stop, so that you can sort a slice in place. In
other words,

x = range(1000000)
x.sort(start=3, stop=-1)

would be equivalent to

x[3:-1] = sorted(x[3:-1])

but more efficient and without repeating the slice twice. If it''s not
too late for feature additions, I''d love to see this in 2.5.




这是一个错误的优化。切片步骤为O(n),并且排序

步骤为O(n log n),除非数据具有某种内部结构,即
Timsort可以用来接近O(n)。


如果你实现了这个并在其真正的应用程序中定时,我会感到惊讶

来发现切片提取和分配是性能

瓶颈。 IMO,不太可能获得性能提升。

雷蒙德



This is a false optimization. The slicing steps are O(n) and the sort
step is O(n log n) unless the data has some internal structure that
Timsort can use to get closer to O(n).

If you implemented this and timed in it real apps, I would be surprised
to find that the slice extraction and assignments were the performance
bottleneck. IMO, a worthwhile performance gain is unlikely.
Raymond


这篇关于功能请求:排序列表切片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆