【发布时间】:2020-05-27 11:47:16
【问题描述】:
第 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。
包括任何一个。
【问题讨论】:
标签: python pandas dictionary tuples range