0

我一直在尝试从单词列表或单词字符串中构造一个问题加权图,以将单词字符串指定给命名元组列表,其中每个可能的单词组合为头和尾,附加头“根”

输入:

['john', 'saw', 'mary', 'root']

所需输出:

[Arc('root',weight,'saw'),Arc('root',weight,'john'),Arc('root', weight,'mary'),
 Arc('saw', weight,'john'), Arc('john', weight, 'saw'), Arc('saw', weight,'mary'),Arc('john', weight,'mary'),
 Arc('mary', weight,'john'),Arc('saw', weight,'mary'),Arc('mary', weight,'saw')]

代码:

from collections import defaultdict, namedtuple 

Arc = namedtuple('Arc', ('head', 'weight', 'tail'))

def Constrain_graph(sentence):
    Arc = namedtuple('Arc', ('head', 'weight', 'tail'))
    C_graph=[]
    for wordindex in range(1, len(sentence)):
        G = nx.DiGraph()
        G.nodes(sentence[wordindex])

        #G.add_node(nod)
        G.add_nodes_from(range(1,len(sentence)))
        C_graph=nx.Graph()
    return C_graph

我需要输出像上面提到的那样

4

0 回答 0