有时候需要对某一组数组的数据进行判断是否 递增 的场景,比如我在开发一些体育动作场景下,某些肢体动作是需要持续朝着垂直方向向上变化,那么z轴的值是会累增的。同理,逆向考虑,递减就是它的对立面。

下面是查找总结到的所有方式,如有补充可以评论区提出。

资料参考来源: Check if list is strictly increasing

1. zip() and all()

  • Code:
test_list = [1, 4, 5, 7, 8, 10]
# Using zip() and all() to
# Check for strictly increasing list
res = all(i < j for i, j in zip(test_list, test_list[1:]))
print(f"Is list strictly increasing ? : {res}")
  • Output:
Is list strictly increasing ? : True

时间复杂度: O(n), n是数组的长度。

2. reduce and lambda

  • Code:
import functools

test_list = [1, 4, 5, 7, 8, 10]
res = bool((lambda list_demo: functools.reduce(lambda i, j: j if
i < j else 9999, list_demo) != 9999)(test_list)) print(f"Is list strictly increasing ? : {res}")
  • Output:
Is list strictly increasing ? : True

时间复杂度: O(n), n是数组的长度。

3. itertools.starmap() + zip() + all()

  • Code:
import itertools

test_list = [1, 4, 5, 7, 8, 10]
res = all(itertools.starmap(operator.le, zip(test_list, test_list[1:]))) print(f"Is list strictly increasing ? : {res}")
  • Output:
Is list strictly increasing ? : True

时间复杂度: O(n), n是数组的长度。

4. sort() and extend()

  • Code:
test_list = [1, 4, 5, 7, 8, 10]
res = False
new_list = []
new_list.extend(test_list)
test_list.sort() if new_list == test_list:
res = True print(f"Is list strictly increasing ? : {res}")
  • Output:
Is list strictly increasing ? : True

时间复杂度: O(nlogn), 这里是sort()的时间复杂度

5. Use stacks

栈是一种后进先出的数据结构(Last in, first out)。

  • Code:
def is_strictly_increasing(lst):
stack = []
for i in lst:
if stack and i <= stack[-1]:
return False
stack.append(i)
return True test_list = [1, 4, 5, 7, 8, 10]
print(is_strictly_increasing(test_list)) # True test_list = [1, 4, 5, 7, 7, 10]
print(is_strictly_increasing(test_list)) # False

时间复杂度: O(n),原数组被遍历了一遍

空间复杂度: O(n),栈可能要存储全部的n个原数组元素

6. numpy()

  • Code:
import numpy as np

def is_increasing(lst):
# Converting input list to a numpy array
arr = np.array(lst) # calculate the difference between adjacent elements of the array
diff = np.diff(arr) # check if all differences are positive
# using the np.all() function
is_increasing = np.all(diff > 0) # return the result
return is_increasing # Input list
test_list = [1, 4, 5, 7, 8, 10] # Printing original lists
print("Original list : " + str(test_list)) result = is_increasing(test_list) print(result)
# True

时间复杂度: O(n)

7. itertools.pairwise() and all()

这里面就等于使用 pairwise() 替代了之前的 zip(list, list[1:])

  • Code:
from itertools import pairwise

# Function
def is_strictly_increasing(my_list):
# using pairwise method to iterate through the list and
# create pairs of adjacent elements. # all() method checks if all pairs of adjacent elements
# satisfy the condition i < j, where i and j
# are the two elements in the pair.
if all(a < b for a, b in pairwise(my_list)):
return True
else:
return False # Initializing list
test_list = [1, 4, 5, 7, 8, 10] # Printing original lists
print("Original list : " + str(test_list)) # Checking for strictly increasing list
# using itertools pairwise() and all() method
res = is_strictly_increasing(test_list) # Printing the result
print("Is list strictly increasing ? : " + str(res))
  • Output:
Original list : [1, 4, 5, 7, 8, 10]
Is list strictly increasing ? : True

时间复杂度: O(n)

数组递增的判断【python实现】的更多相关文章

  1. 刷题3:给定一个数组 nums,判断 nums 中是否存在三个下标 a,b,c数相加等于targe且a,b,c不相等

    题目: 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,下标 ,a ,b , c 对应数相加等于 targe 找出所有满足条件且不重复的三元组下标 解析: ...

  2. 字符数组和string判断是否为空行 NULL和0 namespace变量需要自己进行初始化

    string 可以这样判断空行input !="" 字符数组可以通过判断第一个元素是否为空字符'\0',是的话为空行arrar[0]=='\0':或者用长度strlen(char ...

  3. 判断python对象是否可调用的三种方式及其区别

    查找资料,基本上判断python对象是否为可调用的函数,有三种方法 使用内置的callable函数 callable(func) 用于检查对象是否可调用,返回True也可能调用失败,但是返回False ...

  4. (016)给定一个有序数组(递增),敲代码构建一棵具有最小高度的二叉树(keep it up)

    给定一个有序数组(递增),敲代码构建一棵具有最小高度的二叉树. 因为数组是递增有序的.每次都在中间创建结点,类似二分查找的方法来间最小树. struct TreeNode { int data; Tr ...

  5. 【剑指Offer】构建乘积数组 解题报告(Python)

    [剑指Offer]构建乘积数组 解题报告(Python) 标签(空格分隔): 剑指Offer 题目地址:https://www.nowcoder.com/ta/coding-interviews 题目 ...

  6. Python学习笔记_二维数组的查找判断

    在进行数据处理的工作中,有时只是通过一维的list和有一个Key,一个value组成的字典,仍无法满足使用,比如,有三列.或四列,个数由不太多. 举一个现实应用场景:学号.姓名.手机号,可以再加元素 ...

  7. 二维数组中的查找(python)

    题目描述 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数 ...

  8. js:给定两个数组,如何判断他们的相对应下标的元素类型是一样的

    题目: 给Array对象原型上添加一个sameStructureAs方法,该方法接收一个任意类型的参数,要求返回当前数组与传入参数数组(假定是)相对应下标的元素类型是否一致. 假设已经写好了Array ...

  9. 在Python脚本中判断Python的版本

    引自:http://segmentfault.com/q/1010000000127878 如果是给人读,用 sys.version,如果是给机器比较,用 sys.version_info,如果是判断 ...

  10. 旋转数组的最小数字(python)

    题目描述 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入一个非减排序的数组的一个旋转,输出旋转数组的最小元素. 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋 ...

随机推荐

  1. NativeBuffering,进一步提升字符串的序列化性能

    在<NativeBuffering,一种高性能.零内存分配的序列化解决方案[性能测试篇]>我比较了NativeBuffering和System.Text.Json两种序列化方式的性能,通过 ...

  2. Android Recyclerview的item间距实现

    Recyclerview中,提供了一个方法addItemDecoration给我们用于设置item的分割线 下面提供几个常见的分割线效果 注: 下面的SizeUtils是AndroidUtilCode ...

  3. Java开发者的Python快速进修指南:异常捕获

    在之前的学习中,我们已经讲解了函数和控制流等基本概念.然而,在接触实际业务时,你会发现异常捕获也是必不可少的一部分,因为在Java编程中,异常处理是不可或缺的.Python的异常捕获与Java的异常捕 ...

  4. 痞子衡嵌入式:我当了回华邦电子&恩智浦2023联合技术论坛演讲嘉宾

    「华邦电子(Winbond)」是国际领先的存储器厂商,其串行 NOR Flash 产品在全球市场占有率稳居前列. 11月23日,华邦电子联合「恩智浦(NXP)」在上海搞了场主题为"芯智无限, ...

  5. MySQL锁粒度是什么意思?MySQL锁粒度是什么?

    MySQL锁粒度就是我们通常所说的锁级别.数据库引擎具有多粒度锁定,允许一个事务锁定不同类型的资源. MySQL数据库有三种锁的级别,分别是:页级锁.表级锁 .行级锁. 锁粒度 锁粒度就是我们通常所说 ...

  6. XIAMEN_AMOY

    第一份博客,就发布点轻松的内容吧 微雨的清晨 镜头向着前方 越过山丘 海 是像河一样的海 原来真的有随便抓一把都是贝壳的地方 青色,紫色,黄色 转行去做贝壳项链吧 如果不是去海边 那厦门不是一个很晒的 ...

  7. 给祖传系统做了点 GC调优,暂停时间降低了 90%

    问题描述 公司某规则引擎系统,在每次发版启动会手动预热,预热完成当流量切进来之后会偶发的出现一次长达1-2秒的Young GC(流量并不大,并且LB下的每个节点都会出现该情况) 在这次长暂停之后,每一 ...

  8. 聊一聊Rust的enum

    enum在实际编程中是非常常用的,enum的目的就是为了清晰定义出散落在系统各个角落的相同概念的有限固定值. 一.enum介绍如果是简单定义固定值,我们可以使用常量const.比如 public co ...

  9. 经典卷积神经网络LeNet&AlexNet&VGG

    LeNet LeNet-5是一种经典的卷积神经网络结构,于1998年投入实际使用中.该网络最早应用于手写体字符识别应用中.普遍认为,卷积神经网络的出现开始于LeCun等提出的LeNet网络,可以说Le ...

  10. 技本功|Hive优化之Spark执行引擎参数调优(二)

    Hive是大数据领域常用的组件之一,主要是大数据离线数仓的运算,关于Hive的性能调优在日常工作和面试中是经常涉及的的一个点,因此掌握一些Hive调优是必不可少的一项技能.影响Hive效率的主要有数据 ...