我有一个字典,它提供了从实数元组到标识整数的映射。给定一个元组列表,其中包含的数字在字典中的公差范围内,但不完全等于字典中的值,我想生成相应整数的列表。在
示例:
tdict = {(0.334, 0.333, 0.333):1, (0.167, 0.666, 0.167):2, (0.5, 0.5, 0):3}
tlist = [(0.333, 0.333, 0.333), (0.16667, 0.6666667, 0.17), (0.34, 0.33, 0.33), (0.5001, 0.4999, 0.0)]
tol = 0.01
运行我想要的代码应该会产生结果
^{pr2}$因为每个元组中的所有数字都在tdict
中相应元组的给定公差范围内。我可以通过迭代tdict.keys()
并分别比较每个方法来实现这一点,但我觉得应该有更好的方法。得到这些元组对应的整数的最有效的方法是什么?它不必涉及字典,这在我看来是最自然的。我用的是python3。在
很明显,您希望将三维空间中的点投影到具有特定栅格间距(这与公差值直接相关)的三维栅格上,并创建某种直方图。为自己编写一个投影函数:它以任意的3元素列表/元组(描述空间中某个点的向量)为参数,并将其投影到某个网格点上。你这样做是为了充实你的字典,也是为了把它读出来。此外,关于字典中的键,我认为您应该使用整数元组而不是float,因为我不确定float是否可以完全相同。在
这是一个实现示例:
作用:在三维空间中创建20个随机向量(所有坐标都在0和1之间)。它使用空间中的这些点填充三维栅格。网格在每个维度中的间距为0.3。空间中的这20个点中的每一个都被指定给网格中的某个体素(只是3D像素的一个单词)。每次指定都会将相应体素的计数器增加1(将网格渲染为直方图)。然后,用另一组随机的20个向量来读出体素。这些点再次投影到体素上,但这次计数器只是返回而不是增加。执行测试:
^{pr2}$使用您的数据执行:
{cd1>按要求打印。我没有深入思考过
spacing=3*tolerance
之间的关系。可能是错的。我只知道有一个确定的关系。证明/找到这个公式留给你作为练习:)tlist
的每个点的距离对tdict.keys()
排序。在tdict
中查找它们。在如果您有权访问numpy,则可以使用numpy.allclose检查匹配项:
相关问题 更多 >
编程相关推荐