gpt4 book ai didi

python - 在python中将列表转换为矩阵

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:43:42 27 4
gpt4 key购买 nike

我正在尝试构建一个拼写校正器使用图。

第1步:我用一些书籍作为语料库和Python networkx包构建了一个直接图节点是单词,我在这个图中为每个节点添加一个属性叫做'DISTANCE',代表两个单词之间的距离。

 example:
graph[‘love’][‘you’][‘DISTANCE’] = 37,means, in my corpus,‘love you’ appeals 37 times.
graph[‘you’][‘love’][‘DISTANCE’] = 39,means, in my corpus,‘you love’ appeals 39 times.

显然,graph[‘love’][‘you’]和graph[‘you’][‘love’]是不同的。我的问题是当我完成一些操作时,我得到一个包含列表的列表。像这样,(长度是可变的):

[

[who,whom,whose],
[are,all,],
[that,than,this]

]

每个子列表都包含可能是正确的单词,我的问题是我想将这个列表转换成这个。

 [
[who,are,that],
[who,are,than],
[who,are,this],
[who,all,that],
[who,all,than],
[who,all,this],
[whom,are,that],
[whom,are,than],
[whom,are,this],
[whom,all,that],
[whom,all,than],
[whom,all,this],
[whose,are,that],
[whose,are,than],
[whose,are,this],
[whose,all,that],
[whose,all,than],
[whose,all,this],
]

所以我可以计算 DISTANCE,确定最佳距离。

我是算法新手,你知道哪种算法可以满足这个要求吗?如果您有什么建议可以帮助我使这个拼写更正器更有效,请告诉我。

谢谢!

最佳答案

您可以使用 itertools.product进行转换:

from itertools import product

d = [
['who','whom','whose'],
['are','all'],
['that','than','this']
]

print list(product(*d))

格式化输出:

[
('who', 'are', 'that'),
('who', 'are', 'than'),
('who', 'are', 'this'),
('who', 'all', 'that'),
('who', 'all', 'than'),
('who', 'all', 'this'),
('whom', 'are', 'that'),
('whom', 'are', 'than'),
('whom', 'are', 'this'),
('whom', 'all', 'that'),
('whom', 'all', 'than'),
('whom', 'all', 'this'),
('whose', 'are', 'that'),
('whose', 'are', 'than'),
('whose', 'are', 'this'),
('whose', 'all', 'that'),
('whose', 'all', 'than'),
('whose', 'all', 'this')
]

关于python - 在python中将列表转换为矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36619596/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com