简体   繁体   English

从Python中的数字列表中提取数字顺序

[英]extract a numeric order from a numeric list in Python

Let's say that I have a numeric list: 假设我有一个数字列表:

n=[1,2,3,6,7,20,21,22]

I want to write a code that can detect any numeric order inside the list, in this case will be : (1,2,3) and (6,7) and (20,21,22) 我想编写一个可以检测列表中任何数字顺序的代码,在这种情况下,将是: (1,2,3) and (6,7) and (20,21,22)

Anyone has an idea to do this? 有人有想法吗? By the way, it is in python :) thank you! 顺便说一句,它在python中:)谢谢!

You can iterate through list and for every num check if previous was lesser by 1. If so - you add them both to accumulator list. 您可以遍历列表,对于每一个num都检查前一个是否小于1。如果是,则将它们都添加到累加器列表中。 If not - return accumulator and start new collecting. 如果不是,请返回累加器并开始新的收集。 You did't mark language, so I will not provide actual code, sorry :) 您没有标记语言,所以我将不提供实际代码,抱歉:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM