因此,如果我有一个矩阵(列表列表),其中每一列代表一个唯一的单词,每一行代表一个不同的文档,并且每个条目都是 1 或 0,指示给定列的单词是否存在于给定行的文档。
我想知道的是如何确定所有可能的单词和文档组合,其中多个单词与多个文档相同。结果可能类似于:
[ [[Docid_3, Docid_5], ['word1', 'word17', 'word23']],
[[Docid_3, Docid_9, Docid_334], ['word2', 'word7', 'word23', 'word68', 'word982']],
...
等等每个可能的组合。会喜欢一个提供完整组合集的解决方案和一个只产生不是另一个子集的组合的解决方案,所以从这个例子来看,不是 [[Docid_3, Docid_5], ['word1', 'word17' ]]
因为它是第一个示例的完整子集。
我觉得有一个优雅的解决方案,只是没有想到,啤酒也没有帮助。
谢谢。
最佳答案
- 规范化文本。您只需要由
string.lowercase
组成的字符串。拆分/剥离其他所有内容。 - 制作
set
不在这个范围内。 使用类似这样的东西来获得所有可能的各种规模的分组:
def get_all_lengths_combinations_of(elements): for no_of_items in range(2, len(elements)+1): for items in itertools.combinations(elements, no_of_items) yield items
请记住,您应该能够使用
set.intersection()
像这样的方法:set.intersection(*list_of_sets_to_intersect)
关于python - Python 中确定矩阵中所有可能交点的最佳方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3027925/