检查数组是否在Python中包含递增元素 [英] Check if array contains incrementing elements in Python

查看:472
本文介绍了检查数组是否在Python中包含递增元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查Python中的数组是否包含递增元素,每个元素递增1,并从0开始. [0, 1, 2, 3]应该返回"valid",而例如[0, 1, 3, 4][-1, 0, 1]应该返回无效".

I want to check if an array in Python contains incrementing elements, incremented by one each and starting with 0. So, e.g. [0, 1, 2, 3] should return "valid", whereas e.g. [0, 1, 3, 4] or [-1, 0, 1] should return "not valid".

有没有一种简单的方法可以在Python中实现?也许是内置功能?

Is there an easy way to achieve that in Python? Maybe an built-in function?

推荐答案

如果问题确实如您所描述,则可以使用range轻松解决,就像这样:

If the problem is truly as you describe it, this can be solved trivially using range, like so:

myList = [...]
if myList == list(range(myList[-1] + 1)):
    # Do stuff.

这篇关于检查数组是否在Python中包含递增元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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