Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6338851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:31:42+00:00 2026-05-24T19:31:42+00:00

I’m trying to find the most pythonic way to find out if numbers in

  • 0

I’m trying to find the most pythonic way to find out if numbers in a list are sequential. To give some background, I have a list of numbers gathered that exist in a folder, and I need to find out which numbers are missing.

I gather all of the numbers, and then make another list from the range(beginning, end+1) of what numbers should be there. I very easily made something to show me all of the numbers missing:

missing = [x for x in existingNumbers if x not in shouldBeNumbers]

The problem is that if I print out all of those, there are a lot of numbers that could be condensed (i.e. 1, 2, 3, 4, 7, 10 could be printed as 1-4, 7, 10) because there could be massive amounts of numbers missing.

I’ve tried two approaches:

For both ways, frameRange is range(startFrame, endFrame+1) and frameList is a list generated from what exists currently.

1)

    for x in frameRange:
        if x not in frameList:
            if originalFrame == None:
                originalFrame = x
            elif originalFrame:
                if lastFrame == None:
                    lastFrame = x
                elif lastFrame:
                    if lastFrame == x-1:
                        lastFrame = x
                    else:
                        if originalFrame != lastFrame:
                            missingFrames.append(str(originalFrame)+"-"+str(lastFrame))
                            originalFrame = x
                            lastFrame = x
                        else:
                            missingFrames.append(str(originalFrame))
                            originalFrame = x
                            lastFrame = x
        if x == endFrame:
            if originalFrame != lastFrame:
                missingFrames.append(str(originalFrame)+"-"+str(lastFrame))
                originalFrame = x
                lastFrame = x
            else:
                missingFrames.append(str(originalFrame))
                originalFrame = x
                lastFrame = x

2)

    i = 0
    while i < len(frameRange):
        if frameRange[i] in frameList:
            i += 1
        else:
            if i + 1 < len(frameRange):
                if frameRange[i + 1] in frameList:
                    missingFrames.append(str(frameRange[i]))
                    i += 1
                else:
                    j = 1
                    while frameRange[i+j] not in frameList:
                        aheadFrameNumber = int(str(j))
                        if i + j + 1 < len(frameRange):
                            j += 1
                        else:
                            break
                    missingFrames.append(str(frameRange[i])+"-"+str(frameRange[aheadFrameNumber]))
                    if i + aheadFrameNumber + 1 < len(frameRange):
                        i += aheadFrameNumber + 1
            else:
                missingFrames.append(str(frameRange[i]))

The first way was working, but since it happens on the current frame checking the last, whenever the last frame was gone it wouldn’t append the last missing section to the list. For the second way I had to keep wrapping everything in if statements because I kept getting index exceptions when moving forwards.

I think I have to step back, re-think, and approach it differently. I’m wondering if there is a much better way to do this in python that I haven’t thought about yet because I don’t know the function. Both ways started to get a little out of hand.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T19:31:46+00:00Added an answer on May 24, 2026 at 7:31 pm

    Try something like this

    missing=[]
    numbers.insert(0, 0) # add the minimum value on begining of the list
    numbers.append(41)  # add the maximum value at the end of the list
    for rank in xrange(0, len(numbers)-1):
       if numbers[rank+1] - numbers[rank] > 2:
          missing.append("%s-%s"%(numbers[rank] +1 , numbers[rank+1] - 1))
       elif numbers[rank+1] - numbers[rank] == 2:
          missing.append(str(numbers[rank]+1))
    
    print missing
    

    which for numbers = [1,4,6,10, 12,] and numbers should be present are from 1 to 40 you will have :

    ['2-3', '5', '7-9', '11', '13-40']
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.