As people in the comments say, most of your operations are simple list operation. However, if your data contains ranges (e.g. 3008-3015), it then becomes a different story. The reason is that you need some sort of decoding to what that range means.
I wrote a simple code using only functions (no classes) that will do just that.
matched = False
Check whether a number or a range:
def valueCheck(val):
if "-" in val:
return False
else:
return True
Encode the range into two values (start, end)
def rangeCheck(val):
if valueCheck(val):
return val, val
else:
return val.split("-")
Check whether a number matches the content of a list:
def f_check(new_number, numbers_list):
global matched
for i in numbers_list:
rangeCheck(i)
if int(rangeCheck(i)[0]) <= int(new_number) <= int(rangeCheck(i)[1]):
print "{} matches {}".format(new_number, i)
matched = True
break
if not matched:
print "{} doesn't exist in your list".format(new_number)
Add a number to the list:
def f_add(new_number, numbers_list):
numbers_list.append(new_number)
print numbers_list
Delete a number from the list:
def f_delete(new_number, numbers_list):
numbers_list.remove(new_number)
print numbers_list
Assuming your numbers are string values in a list like so:
numbers = ["3000", "3008-3015", "3020", "3022", "3030-3043", "3068"]
No match:
f_check("2000", numbers)
2000 doesn't exist in your list
Single match:
f_check("3020", numbers)
3020 matches 3020
Range match:
f_check("3010", numbers)
3010 matches 3008-3015
Other simple operations:
Add:
f_add("2000", numbers)
['3000', '3008-3015', '3020', '3022', '3030-3043', '3068', '2000']
Delete:
f_delete("3068", numbers)
['3000', '3008-3015', '3020', '3022', '3030-3043']
Please note that single match can also be done much easier by using the following:
number = "3020"
if number in numbers:
print "{} matched".format(number)
3020 matched
UPDATE #1
To overcome your raised "problem" of adding number to existing ranges and/or making new range if needed. I found a similar question here How to group list of continuous values in ranges, which can solve part of your issue. However, your original encoding (val-val) isn't going to be helpful in this case.
To solve this, you can do the following:
Step 1, comment these two lines:
# rangeCheck(i)
# if int(rangeCheck(i)[0]) <= int(new_number) <= int(rangeCheck(i)[1]):
"""
you will not be using the rangeCheck() or valueCheck() anymore.
"""
Step 2, add this line instead of the original IF-statement:
if int(i[0]) <= int(new_number) <= int(i[1]):
Step 3, add this function which will flatten your list of numbers
def flatList(numbers_list):
for i in numbers_list:
if len(str(i).split("-")) > 1:
numbers_list.extend(range(int(i.split("-")[0]), int(i.split("-")[1]) + 1))
numbers_list.remove(i)
return numbers_list
Step 4, add this function (taken from 1) which will group your flat list into ranges of values
from operator import itemgetter
from itertools import groupby
def numbers_group(flatt_list):
flatt_list = [int(i) for i in flatt_list]
ranges = []
for k, g in groupby(enumerate(flatt_list), lambda (i, x): i - x):
group = map(itemgetter(1), g)
ranges.append((group[0], group[-1]))
return ranges
Usage:
numbers = ["3000", "3008-3015", "3020", "3022", "3030-3043", "3068"]
print flatList(numbers)
['3000', '3020', '3022', '3068', 3008, 3009, 3010, 3011, 3012, 3013, 3014, 3015, 3030, 3031, 3032, 3033, 3034, 3035, 3036, 3037, 3038, 3039, 3040, 3041, 3042, 3043]
print numbers_group(sorted(flatList(numbers)))
[(3008, 3015), (3030, 3043), (3000, 3000), (3020, 3020), (3022, 3022), (3068, 3068)]
numbers = f_add("3021", numbers)
print numbers_group(sorted(flatList(numbers)))
[(3008, 3015), (3030, 3043), (3000, 3000), (3020, 3022), (3068, 3068)]
numbers = f_delete("3021", numbers)
print numbers_group(sorted(flatList(numbers)))
[(3008, 3015), (3030, 3043), (3000, 3000), (3020, 3020), (3022, 3022), (3068, 3068)]