当前位置: 首页 > news >正文

质量好网站建设商家环球资源网官方网站

质量好网站建设商家,环球资源网官方网站,莱芜做网站建设的公司,有没有哪个网站怎么做动漫新闻的有时候需要对某一组数组的数据进行判断是否 递增 的场景,比如我在开发一些体育动作场景下,某些肢体动作是需要持续朝着垂直方向向上变化,那么z轴的值是会累增的。同理,逆向考虑,递减就是它的对立面。 下面是查找总结到…

有时候需要对某一组数组的数据进行判断是否 递增 的场景,比如我在开发一些体育动作场景下,某些肢体动作是需要持续朝着垂直方向向上变化,那么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 functoolstest_list = [1, 4, 5, 7, 8, 10]
res = bool((lambda list_demo: functools.reduce(lambda i, j: j ifi < 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 itertoolstest_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 = Trueprint(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 Falsestack.append(i)return Truetest_list = [1, 4, 5, 7, 8, 10]
print(is_strictly_increasing(test_list))  # Truetest_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 npdef is_increasing(lst):# Converting input list to a numpy arrayarr = np.array(lst)# calculate the difference between adjacent elements of the arraydiff = np.diff(arr)# check if all differences are positive# using the np.all() functionis_increasing = np.all(diff > 0)# return the resultreturn 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 Trueelse: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)

http://www.15wanjia.com/news/41762.html

相关文章:

  • 海尔集团网站是怎么做的十大计算机培训机构排名
  • 专业网站制作公司招聘站长工具seo综合查询论坛
  • 网站建设进度的问题百度关键词搜索排名多少钱
  • uniapp微信小程序模板泉州seo外包
  • 做产品推广有网站比较好的html做一个简单的网页
  • wordpress主题对应的插件外贸网站seo优化
  • 云建站优势网络营销师是干什么的
  • 重庆网站建设招聘安徽seo优化规则
  • 电子商务网站建设作品网络服务器是指什么
  • 外包做网站要十几万免费建设网站平台
  • 成都网站建设排行榜整合营销包括哪三方面
  • wordpress付费主题推荐seo公司赚钱吗
  • 上海技术做网站微博上如何做网站推广
  • 网站建设优化去哪学微信如何投放广告
  • 开发网站的财务分析西安seo优化工作室
  • 用vs2010做网站css东莞建设企业网站
  • 网站建设找客户春哥seo博客
  • 专门做财经的网站彩虹云商城网站搭建
  • wordpress主机和域名绑定域名保定seo网络推广
  • 学院网站板块朋友圈广告怎么投放
  • 一般网站开发好的框架都有哪些百度竞价多少钱一个点击
  • 购物网站的详细设计免费建网站知乎
  • 济南网站地址滨州seo招聘
  • wordpress双语安装天津seo优化公司哪家好
  • 青年旅舍网站开发背景及意义百度地图人工电话
  • 婺城区建设局网站产品线上营销推广方案
  • 政府机关网站备案网站分析案例
  • 浙江建站管理系统价格seo网络优化前景怎么样
  • 西宁网站优化网络营销工具体系
  • 个人注册一个小公司要多少钱搜索引擎优化网页