在 Python 中,如何将一系列值映射到一个具体值? 基本上,我想要一本字典,我可以用数字填充范围和索引:
rd = rangedict()
rd[(0, 10)] = 5
print rd[4] # prints 5
print rd[6] # prints 5
rd[(5, 15)] = 20
print rd[4] # prints 5
print rd[6] # prints 20
最佳答案
你可以使用区间树
pip 安装间隔树
from intervaltree import Interval, IntervalTree
rd = IntervalTree()
rd[0:10] = 5
print rd[4]
print rd[5]
关于python - 以范围为键的字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34717934/