2012-04-27 52 views
0

我建立添加了一些nametuple

Corpus = collections.namedtuple('Corpus', 'a, b, c, d') 

閱讀語料庫中的所有文件,並保存數據,

def compute(counters, tokens, catergory) 
    ... 
    counters.stats[tokens][catergory] = Corpus(a, b, c, d) 

令牌和CATERGORY是collection.Counter()。在閱讀counters.stats中的a,b,c,d中的所有信息後,我在另一個函數中進行一些計算,併爲每個標記獲取'e'。如何將e添加到此函數的counters.stats中?

+2

''這是[category](http://dictionary.reference.com/browse/category),而不是catergory。 '' – 2012-04-27 21:20:41

回答

3

如果您正在討論將'e'添加到counter.stats[tokens][category]的語料庫中,那麼這是不可能的,因爲namedtuples是不可變的。您可能必須使用a b c d e值創建一個新的namedtuple,並將其分配給counter.stats [tokens] [category]。下面的代碼是一個例子:

>>> from collections import namedtuple 
>>> two_d = namedtuple('twoDPoint', ['x', 'y']) 
>>> x = two_d(1, 2) 
>>> x = two_d(1, 2) 
>>> three_d = namedtuple('threeDPoint', ['x', 'y', 'z']) 
>>> x 
twoDPoint(x=1, y=2) 
>>> y = three_d(*x, z=3) 
>>> y 
threeDPoint(x=1, y=2, z=3) 
相關問題