Python Forum
[SOLVED] [loop] Exclude ranges in… range?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[SOLVED] [loop] Exclude ranges in… range?
#1
Hello,

I need to exclude pages in a loop, but those exceptions could be several pages, so I need to find a way to specify individual pages as well as a range of pages:

numpages = doc.page_ount
#TODO How to use ranges in range, eg. [1-14,25,35-41,57]?
exclude = [1,2,3,4,5]
for index in numpages:
    if not exclude[index]:
        #do stuff
Do you know of a way to do this?

Thank you.
Reply
#2
Something like this maybe:

my_ranges = [range(1, 10), range(25, 30), range(35, 41)]
exclude = [1, 2, 3, 4, 5]

for r in my_ranges:
    for i in r:
        if i not in exclude:
            print(i)
/regards
Winfried likes this post
Reply
#3
Thanks!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] Loop through directories and files one level down? Winfried 3 56 1 hour ago
Last Post: Gribouillis
  Regex Include and Exclude patterns in Same Expression starzar 2 808 May-23-2023, 09:12 AM
Last Post: Gribouillis
  Loop through json file and reset values [SOLVED] AlphaInc 2 2,153 Apr-06-2023, 11:15 AM
Last Post: AlphaInc
  Delete all Excel named ranges (local and global scope) pfdjhfuys 2 1,808 Mar-24-2023, 01:32 PM
Last Post: pfdjhfuys
  [SOLVED] [Linux] Script in cron stops after first run in loop Winfried 2 937 Nov-16-2022, 07:58 PM
Last Post: Winfried
  [SOLVED] [BS] Why new tag only added at the end when defined outside the loop? Winfried 1 980 Sep-05-2022, 09:36 AM
Last Post: snippsat
  Loop through list of ip-addresses [SOLVED] AlphaInc 7 4,007 May-11-2022, 02:23 PM
Last Post: menator01
  [Solved] Matplotlib - Tricontour: how to set colorbar range ju21878436312 1 6,638 Dec-13-2021, 07:44 PM
Last Post: ju21878436312
  matplotlib x axis range goes over the set range Pedroski55 5 3,233 Nov-21-2021, 08:40 AM
Last Post: paul18fr
  Dictionary with ranges that have a float step value Irv1n 2 2,128 Apr-21-2021, 09:04 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020