0

私は造る

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

コーパス内のすべてのファイルを読み取り、データを保存し、

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

トークンとカテゴリの両方が collection.Counter() です。counters.stats の a、b、c、d のすべての情報を読み取った後、別の関数で計算を行い、トークンごとに「e」を取得します。この関数の counters.stats に e を追加するにはどうすればよいですか?

4

1 に答える 1

3

コーパスのnamedtupleに「e」を追加することについて話している場合counter.stats[tokens][category]、namedtupleは不変であるため、それは不可能です。値を使用して新しい名前付きタプルを作成し、a b c d eそれを 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)
于 2012-04-27T21:49:47.440 に答える