检查列表是否已排序的 Pythonic 方法 [英] Pythonic way to check if a list is sorted or not

查看:30
本文介绍了检查列表是否已排序的 Pythonic 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种pythonic的方法来检查列表是否已经在ASCDESC

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

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

类似于 isttimestamps.isSorted() 的东西,它返回 TrueFalse.

something like isttimestamps.isSorted() that returns True or False.

我想输入一些消息的时间戳列表,并检查交易是否以正确的顺序出现.

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

推荐答案

实际上,我们并没有给出 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 3:

For Python 3:

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

这篇关于检查列表是否已排序的 Pythonic 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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