Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
compacting multiple ranges
#1
i have two or more int, float, Decimal, string, or bytes (anything simple and sortable) non-overlapping ranges, like maybe:
1...1000, 2000...4000, 9000....
or maybe:
'alice'...'carol', 'wanda'...'zelda'

i want to apply a value test across the ranges so that a return value associated with each range is return (or yielded)
so in the above examples i might have:
1...1000:1, 2000...4000:2, 9000....:3 default:0
'alice'...'carol':400, 'wanda'...'zelda':800 default:600

i'm curious what tools are commonly used or what coding style/pattern is used when trying to map a value that could be in one of the ranges to a value (maybe the same type or maybe different) associated with the range it is in.

for functions doing this perhaps a way to represent these ranges with a mapped value could have integrated values like:
[(1,1,1000),(2,2000,4000)(3,9000),0]
[(400,'alice',carol'),(800,'wanda','zelda'),600]

or separated values like:
[(1,1000),(2000,4000)(9000,)] and [1,2,3,0]
[('alice',carol'),('wanda','zelda')] and [400,800,600]

or the default value could be given as a separate argument.

any suggestions or ideas?  i'm sure a need for this have come up many times, already.
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
With such a small set of output values, the easiest approach is if-then-else statements:
Pseudocode:

if <s in range_1>:
return 1
elif <s in range_2>:
return 2
...

<s in range_1> could be implemented as one of these:

s in ('Alice',...,'Carol') # tuple
s in ['Alice',...,'Carol'] #list

If you had a much larger set of output values, it would be best to use a dict:
range_1 = {'Alice': 1,...,'Carol:1}

If you have both a reasonably large set of input and output values, you can just use a dict:
range = {'Alice':1,...,'Wanda':93}
Reply
#3
i am looking for a function that can do a variable number of ranges and can work with any type that can compare with another of the same type that can be compared with < and/or > (including immutable sequences).
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] [loop] Exclude ranges in… range? Winfried 2 1,367 May-14-2023, 04:29 PM
Last Post: Winfried
  Delete all Excel named ranges (local and global scope) pfdjhfuys 2 1,689 Mar-24-2023, 01:32 PM
Last Post: pfdjhfuys
  Dictionary with ranges that have a float step value Irv1n 2 2,073 Apr-21-2021, 09:04 PM
Last Post: Yoriz
  Two operations in two ranges salwa17 3 2,092 Jun-22-2020, 04:15 PM
Last Post: perfringo
  iterating a list of ranges Skaperen 1 1,991 May-22-2019, 07:44 AM
Last Post: Gribouillis
  Subnet Mask Ranges ab52 0 1,785 Mar-11-2019, 10:39 AM
Last Post: ab52
  joined ranges Skaperen 4 3,164 Apr-03-2018, 07:14 PM
Last Post: Gribouillis
  compacting a mutable sequence Skaperen 6 4,343 Jan-23-2018, 03:54 AM
Last Post: Skaperen
  a dictionary of ranges Skaperen 10 6,048 Dec-02-2017, 11:29 PM
Last Post: Windspar

Forum Jump:

User Panel Messages

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