关联矩阵?

发布于 2024-08-21 09:10:33 字数 294 浏览 6 评论 0原文

我正在开发一个项目,需要存储由两个字符串键索引的数字矩阵。矩阵不是锯齿状的,即如果任何行存在列键,那么所有行都应该存在该列键。类似地,如果任何列都存在行键,那么所有列都应该存在该行键。

表达这一点的明显方法是使用关联数组的关联数组,但这既笨拙又低效,并且它不强制执行非锯齿属性。是否有流行的编程语言提供关联矩阵,或者内置到该语言中,或者作为其标准库的一部分?如果是这样,它们在 API 和实现层面上如何工作?我在这个项目中使用 Python 和 D,但其他语言的示例仍然有用,因为我能够查看 API 并找出在 Python 或 D 中实现类似功能的最佳方法。

I'm working on a project where I need to store a matrix of numbers indexed by two string keys. The matrix is not jagged, i.e. if a column key exists for any row then it should exist for all rows. Similarly, if a row key exists for any column then it should exist for all columns.

The obvious way to express this is with an associative array of associative arrays, but this is both awkward and inefficient, and it doesn't enforce the non-jaggedness property. Do any popular programming languages provide an associative matrix either built into the language or as part of their standard libraries? If so, how do they work, both at the API and implementation level? I'm using Python and D for this project, but examples in other languages would still be useful because I would be able to look at the API and figure out the best way to implement something similar in Python or D.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

泪痕残 2024-08-28 09:10:33

为什么不只使用标准矩阵,而是使用两个字典 - 一个将行键转换为行索引,另一个将列键转换为列索引。我认为你可以很容易地创建自己的结构,以这种方式工作。您只需创建一个包含矩阵和两个字典的类,然后从那里开始。

Why not just use a standard matrix, but then have two dictionaries - one that converts the row keys to row indices and one that converts the columns keys to columns indices. You could make your own structure that would work this way fairly easily I think. You just make a class that contains the matrix and the two dictionaries and go from there.

风渺 2024-08-28 09:10:33

在Python中,你可以有一个由两个字符串的元组索引的字典,例如

>>> d = {}
>>> d["foo","bar"] = 10
>>> d
{('foo', 'bar'): 10}

我不确定“强制非锯齿”对你意味着什么,但是你可以使用defaultdict为尚未明确显示的条目返回默认值设置或使用已知值初始化字典:

>>> xkeys = "abcdef"
>>> ykeys = "xyz"
>>> d = dict(((x,y), 0) for x in xkeys for y in ykeys)
>>> d
{('b', 'y'): 0, ('a', 'z'): 0, ('b', 'x'): 0, ('e', 'y'): 0, ('a', 'x'): 0, ('f', 'z'): 0, ('a', 'y'): 0, ('f', 'y'): 0, ('d', 'y'): 0, ('f', 'x'): 0, ('d', 'x'): 0, ('e', 'x'): 0, ('e', 'z'): 0, ('c', 'x'): 0, ('d', 'z'): 0, ('c', 'y'): 0, ('c', 'z'): 0, ('b', 'z'): 0}

如果您想强制只允许已知集合中的键,那么我建议对 dict 进行子类化以添加验证。

In Python you could have a dict indexed by a tuple of two strings, e.g

>>> d = {}
>>> d["foo","bar"] = 10
>>> d
{('foo', 'bar'): 10}

I am not sure what "enforce non-jaggedness" means for you, but you could either use a defaultdict to return a default value for entries that have not been explicitly set, or initialise the dict with the a known value:

>>> xkeys = "abcdef"
>>> ykeys = "xyz"
>>> d = dict(((x,y), 0) for x in xkeys for y in ykeys)
>>> d
{('b', 'y'): 0, ('a', 'z'): 0, ('b', 'x'): 0, ('e', 'y'): 0, ('a', 'x'): 0, ('f', 'z'): 0, ('a', 'y'): 0, ('f', 'y'): 0, ('d', 'y'): 0, ('f', 'x'): 0, ('d', 'x'): 0, ('e', 'x'): 0, ('e', 'z'): 0, ('c', 'x'): 0, ('d', 'z'): 0, ('c', 'y'): 0, ('c', 'z'): 0, ('b', 'z'): 0}

If you want to enforce that only keys in a known set are allowed then I suggest subclassing dict to add the validation.

梦中的蝴蝶 2024-08-28 09:10:33

python 的 larry 模块最近发布了。我相信它能达到你想要的效果。

the larry module for python was recently released. i believe it does what you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文