Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
joined ranges
#1
given 4 ints which define 2 ranges, i can do
foo = [x for x in range(a,b)]+[x for x in range(c,d)]
but it would be nice if there was a more compact form like
foo = range(a,b,1,c,d,1) # warning: invalid code
anyone know of a compact way to join multiple ranges preferably in a range-like object? otherwise, i'm thinking of making a generator to do this.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
There is
foo = itertools.chain(range(a, b), range(c, d))
Reply
#3
the idea i had, but now no longer care to do was a generator like ranges((aa,az[,ai]),(ba,bz[,bi]),...).
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
Isn't this something a list comprehension can easily solve?

>>> ranges = lambda a, b: (i for items in (a, b) for i in items)
>>> ranges(range(2, 9, 2), range(5))
<generator object <lambda>.<locals>.<genexpr> at 0x031EC450>
>>> list(ranges(range(2, 9, 2), range(5)))
[2, 4, 6, 8, 0, 1, 2, 3, 4]
Reply
#5
(Apr-03-2018, 06:30 PM)nilamo Wrote: Isn't this something a list comprehension can easily solve?
Have a look at itertools.chain()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] [loop] Exclude ranges in… range? Winfried 2 1,475 May-14-2023, 04:29 PM
Last Post: Winfried
  Delete all Excel named ranges (local and global scope) pfdjhfuys 2 1,807 Mar-24-2023, 01:32 PM
Last Post: pfdjhfuys
  Dictionary with ranges that have a float step value Irv1n 2 2,125 Apr-21-2021, 09:04 PM
Last Post: Yoriz
  Two operations in two ranges salwa17 3 2,154 Jun-22-2020, 04:15 PM
Last Post: perfringo
  iterating a list of ranges Skaperen 1 2,041 May-22-2019, 07:44 AM
Last Post: Gribouillis
  Subnet Mask Ranges ab52 0 1,825 Mar-11-2019, 10:39 AM
Last Post: ab52
  a dictionary of ranges Skaperen 10 6,147 Dec-02-2017, 11:29 PM
Last Post: Windspar
  compacting multiple ranges Skaperen 2 3,113 Oct-11-2017, 08:33 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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