Python化的方法来检查,如果一个列表进行排序或不 [英] Pythonic way to check if a list is sorted or not

查看:106
本文介绍了Python化的方法来检查,如果一个列表进行排序或不的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个Python化的方法来检查,如果列表已经按在AESC或DESC。

Is there a pythonic way to check if a list is already sorted in AESC or DESC.

listtimestamps=[1,2,3,5,6,7]

像→ isttimestamps.isSorted()返回

编辑:我想输入时间戳某些邮件列表,并检查是否在交易出现在正确的顺序

I want to input a list of timestamps for some messages and check if the the transactions appeared in the correct order.

和我可以写一个自定义的功能太:),谢谢

and I can write a custom function too :) , Thanks

编辑:感谢您指出驼峰问题:)

Thanks for pointing out camel case issues :)

编辑:另外我很抱歉,如果我没说清楚,我不需要事后排序,我只需要检查列表进行排序,我用它来解决决策问题

Also I am sorry if I wasn't clear I don't need to sort it afterwards, I just need to check if the list is sorted, I am using it to solve a decision problem

推荐答案

其实我们不是给答案anijhaw所期待的。这里是单行:

Actually we are not giving the answer anijhaw is looking for. Here is the one liner:

all(l[i] <= l[i+1] for i in xrange(len(l)-1))

这篇关于Python化的方法来检查,如果一个列表进行排序或不的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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