2015-07-20 71 views
-1
class node: 
    def __init__(self, parent, daughters, edge): 
     self.parent = parent 
     self.daughters = daughters 
     self.edge = edge 
     trie.append(self) 
     self.index = len(trie) - 1 
     trie[parent].daughters.append(self.index) 
    ... 

    def SuffixTreeConstruction(): 
    global trie 
    print(len(trie)) 
    for node in trie: 
     if len(node.daughters) == 1: 
      node.edge = ''.join([node.edge, trie[node.daughters[0]].edge]) 
      ... 

我想取两个不同节点的边缘,并将它们组合为一个字符串。边缘是字符串的迭代(for base in text: create node with base as edge),所以我假设它们是单个字符串,而不是字符(带有数字值)。但是,它们显然是整数。这有什么明显的原因吗?为什么连接两个字符串会产生类型错误?

Traceback (most recent call last): 
    File "trieMatching.1.py", line 149, in <module> 
    SuffixTreeConstruction() 
    File "trieMatching.1.py", line 106, in SuffixTreeConstruction 
    node.edge = ''.join([node.edge, trie[node.daughters[0]].edge]) 
TypeError: sequence item 1: expected str instance, int found 
+0

好像你有一个int,而不是一个字符串 – user3636636

回答

1

从误差,可能的是,无论是node.edge或特里结构的[node.daughters [0]]。边缘]是类型<type 'int'> 因此,尝试通过将它们类型转换成字符串,

node.edge = ''.join([str(node.edge), str(trie[node.daughters[0]].edge])) 
0

另一种方式,以避免类型转换是使用format字符串来得到相同的结果”

node.edge = '{0}{1}'.format(node.edge, trie[node.daughters[0]].edge) 

的类型转换会为你完成。增加的benfit是THA你可以格式化东西无论如何你喜欢...看到formatdocs and examples

相关问题