Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中将列表转换为矩阵_Python_Algorithm_Matrix_Graph - Fatal编程技术网

在python中将列表转换为矩阵

在python中将列表转换为矩阵,python,algorithm,matrix,graph,Python,Algorithm,Matrix,Graph,我正在尝试建立一个拼写更正使用图 步骤1:我使用一些书籍作为语料库和Python networkx包来构建一个直接图节点is word,并在这个图中为每个节点添加一个名为“DISTANCE”的属性,表示两个单词之间的距离 example: graph[‘love’][‘you’][‘DISTANCE’] = 37,means, in my corpus,‘love you’ appeals 37 times. graph[‘you’][‘love’][‘DISTANCE’] = 39,me

我正在尝试建立一个拼写更正使用图

步骤1:我使用一些书籍作为语料库和Python networkx包来构建一个直接图节点is word,并在这个图中为每个节点添加一个名为“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.
显然,图['love']['you']和图['you']['love']是不同的。 我的问题是当我完成一些操作时,我得到了一个包含列表的列表。 这样,(长度是可变的):

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

 [
 [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],
 ]
所以我可以计算距离,确定最佳距离

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

谢谢

您可以使用进行转换:

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')
]
您可以使用进行转换:

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')
]

非常感谢,此功能运行得非常好~非常感谢,此功能运行得非常好~