简体   繁体   中英

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)

Anyone has an idea to do this? By the way, it is in python :) thank you!

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. If not - return accumulator and start new collecting. You did't mark language, so I will not provide actual code, sorry :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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