How do modify the inclusion/exclusion behavior of Boundary values in the Range function?
Question:
Part1:
I have a dictionary as:
mydict = {'K': {'VAL1': 'apple', 'VAL2': (60, 80)},
'L': {'VAL1': 'mango', 'VAL2': (90, 100)},
'M': {'VAL1': 'pears', 'VAL2': (120, 150)}}
rto = tuple(range(60,80)) # works
rto = tuple(range(mydict['K']['VAL2']))
TypeError: range() integer end argument expected, got list.
How do I make this work, i wana iterate through the dictionary ??
Part2:
Assuming the above can work, I want to check if a value is in the range:
my_value = 70
rto = tuple(range(60,80))
if my_value in rto :
print("Value is in range")
else:
print("Value not in range")
# Output:
# 70- Value is in range
# 20- Value not in range
# 60- Value is in range
# 80- Value not in range
# (This tells me that the range function includes 60 and excludes 80 from the
# test)
How can I manipulate the boundary conditions for the test? Meaning either:
Include both 60 and 80.
Exclude both 60 or 80.
Include either one.
Answers:
I don’t think you need to create a range, since you can run a check between the values to get your solution.
The code below uses the less than and greater than operator to find if the my_value exists between the tuples:
for i in [j['VAL2'] for i,j in mydict.items()]:
if i[0] <= my_value <= i[-1]:
print(f'{i},{my_value} value is in range')
else:
print(f'{i},{my_value} value is not in range')
You can do this for the dict. See here.
rto = tuple(range(*mydict['K']['VAL2']))
Part1:
I have a dictionary as:
mydict = {'K': {'VAL1': 'apple', 'VAL2': (60, 80)},
'L': {'VAL1': 'mango', 'VAL2': (90, 100)},
'M': {'VAL1': 'pears', 'VAL2': (120, 150)}}
rto = tuple(range(60,80)) # works
rto = tuple(range(mydict['K']['VAL2']))
TypeError: range() integer end argument expected, got list.
How do I make this work, i wana iterate through the dictionary ??
Part2:
Assuming the above can work, I want to check if a value is in the range:
my_value = 70
rto = tuple(range(60,80))
if my_value in rto :
print("Value is in range")
else:
print("Value not in range")
# Output:
# 70- Value is in range
# 20- Value not in range
# 60- Value is in range
# 80- Value not in range
# (This tells me that the range function includes 60 and excludes 80 from the
# test)
How can I manipulate the boundary conditions for the test? Meaning either:
Include both 60 and 80.
Exclude both 60 or 80.
Include either one.
I don’t think you need to create a range, since you can run a check between the values to get your solution.
The code below uses the less than and greater than operator to find if the my_value exists between the tuples:
for i in [j['VAL2'] for i,j in mydict.items()]:
if i[0] <= my_value <= i[-1]:
print(f'{i},{my_value} value is in range')
else:
print(f'{i},{my_value} value is not in range')
You can do this for the dict. See here.
rto = tuple(range(*mydict['K']['VAL2']))