2016-04-14 16 views
0

Ich versuche, eine Rechtschreibkorrektur verwenden Diagramm verwenden.konvertieren Sie eine Liste in Matrix in Python

Schritt 1: Ich verwende einige Bücher als Corpus und Python NetworkX-Paket, um einen direkten Grafikknoten zu erstellen, ist Wort, und ich füge ein Attribut in diesem Diagramm für jeden Knoten mit dem Namen 'DISTANCE', repräsentiert den Abstand zwischen zwei Wörtern.

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. 

Offenbar graph [ ‚Liebe‘] [ ‚Sie‘] und Grafik [ ‚Sie‘] [ ‚Liebe‘] ist anders. Mein Problem ist, wenn ich eine Operation abgeschlossen habe, habe ich eine Liste enthält Liste. Wie diese (die Länge ist variabel):

[ 

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

] 

Jede Unterwortliste enthalten, die richtige sein könnte, mein Problem ist, ich diese Liste diese konvertieren möchten.

[ 
[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], 
] 

So kann ich die Entfernung berechnen, die beste zu bestimmen.

Ich bin ein sehr neuer Typ im Algorithmus, wissen Sie, welcher Algorithmus diese Anfrage erfüllen kann? Und wenn Sie einen Vorschlag haben, könnte mir helfen, diesen Rechtschreibkorrektor effektiver zu machen, lassen Sie es mich wissen.

Danke!

Antwort

3

Sie können itertools.product verwenden, um die Konvertierung zu tun:

from itertools import product 

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

print list(product(*d)) 

Formatierte Ausgabe:

[ 
    ('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') 
] 
+0

Vielen Dank, diese Funktion so gut funktioniert ~ – Fudun

Verwandte Themen