首页 > 编程语言 >数组递增的判断【python实现】

数组递增的判断【python实现】

时间:2023-05-23 15:15:02浏览次数:47  
标签:复杂度 python res 递增 list 数组 test increasing strictly

有时候需要对某一组数组的数据进行判断是否 递增 的场景,比如我在开发一些体育动作场景下,某些肢体动作是需要持续朝着垂直方向向上变化,那么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,res,递增,list,数组,test,increasing,strictly
From: https://www.cnblogs.com/cpl9412290130/p/17425223.html

相关文章

  • Python语言中__init__与__new__的区别是什么?
    __new__和__init__二者都是Python面向对象语言中的函数,其中__new__比较少用,__init__相对常用,那么两者有什么区别呢?以下是详细的内容:__new__作用:创建对象,并分配内存__init__作用:初始化对象的值注意:1、与java相比,java只有一个构造器。而python__new__方法与__in......
  • 音容笑貌,两臻佳妙,人工智能AI换脸(deepfake)技术复刻《卡萨布兰卡》名场面(Python3.1
    影史经典《卡萨布兰卡》是大家耳熟能详的传世名作,那一首壮怀激烈,激奋昂扬的马赛曲,应当是通片最为激动人心的经典桥段了,本次我们基于faceswap和so-vits库让AI川普复刻美国演员保罗·亨雷德高唱《马赛曲》的名场面。配置人脸替换DeepFakes项目关于人脸替换,业内鼎鼎有名的deepfakes......
  • (转载)PYTHON修饰器的函数式编程--纪念下陈皓老师
    PYTHON修饰器的函数式编程2014年的时候陈老师就写了这个python装饰器的文章,比现在的很多的文章都好谨以此文纪念下陈皓老师Python的修饰器的英文名叫Decorator,当你看到这个英文名的时候,你可能会把其跟DesignPattern里的Decorator搞混了,其实这是完全不同的两个东西。虽然好......
  • Python从0到1丨带你认识图像平滑的三种线性滤波
    摘要:常用于消除噪声的图像平滑方法包括三种线性滤波(均值滤波、方框滤波、高斯滤波)和两种非线性滤波(中值滤波、双边滤波),本文将详细讲解三种线性滤波方法。本文分享自华为云社区《[Python从零到壹]五十五.图像增强及运算篇之图像平滑(均值滤波、方框滤波、高斯滤波)》,作者:eastmount。......
  • Python从0到1丨带你认识图像平滑的三种线性滤波
    摘要:常用于消除噪声的图像平滑方法包括三种线性滤波(均值滤波、方框滤波、高斯滤波)和两种非线性滤波(中值滤波、双边滤波),本文将详细讲解三种线性滤波方法。本文分享自华为云社区《[Python从零到壹]五十五.图像增强及运算篇之图像平滑(均值滤波、方框滤波、高斯滤波)》,作者:eastmount......
  • FZU 2236(离散化+树状数组)
    【离散化】借此题记一下离散化。离散化:当题目数据很大时,但数的个数不多,可以采用离散化,降低数值,便于计算。例如数列{89,14,9,1000,2};离散化后:{4,3,2,5,1};(此操作后,数值整体降低,甚至可以当数组下标使用了)具体操作参见本题代码。离散化三部曲:1.数组ha[]存储所有存在过的......
  • 图解LeetCode——1460. 通过翻转子数组使两个数组相等(难度:简单)
    一、题目给你两个长度相同的整数数组 target 和 arr 。每一步中,你可以选择 arr 的任意非空子数组 并将它翻转。你可以执行此过程任意次。如果你能让arr 变得与target 相同,返回True;否则,返回False。二、示例2.1>示例1:【输入】target=[1,2,3,4],arr=[2,4,1,3]......
  • 图解LeetCode——1441. 用栈操作构建数组(难度:中等)
    一、题目给你一个数组target和一个整数n。每次迭代,需要从 list={1,2,3...,n}中依次读取一个数字。请使用下述操作来构建目标数组target:"Push":从list中读取一个新元素,并将其推入数组中。"Pop":删除数组中的最后一个元素。如果目标数组构建完成,就停止读取更多元......
  • Python 操作ES
    1.安装pip参考地址:https://blog.51cto.com/wangshiyu/5267092要指定版本,否则运行代码回报错。pipinstallelasticsearch==7.13.0-ihttp://pypi.doubanio.com/simple/--trusted-hostpypi.doubanio.com2.代码fromelasticsearchimportElasticsearches=Elastic......
  • python 迭代器
    迭代器迭代器的理解,学过Java的都是知道的,java也是都是由迭代器的,而且是一种设计思想,实际上。可以把迭代器理解成一种特殊的list+function实际上,迭代器是一个特殊的函数迭代器的特性可迭代的使用next这个内置函数来移动内部的下一个指针通过for遍历的时候实际也是调用n......