2015-07-20 27 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