如何在范围函数中修改边界值的包含/排除行为?

flying_fluid_four

第1部分:我的字典如下:

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.

我要如何遍历字典?

第2部分:假设上述方法可行,我想检查一个值是否在范围内:

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)

如何操纵测试的边界条件?含义之一:
包括60和80。
排除60或80。
包括一个。

sammywemmy

我认为您无需创建范围,因为您可以在值之间运行检查以获取解决方案。

下面的代码使用小于和大于运算符查找在元组之间是否存在my_value。让我知道您是否打算这样做。

 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')

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章