how I can reset collections.counter in Python? -


i want reset ( count 0 ) in list need reset counter ( count 0 ) :

enter code here  inread in content.splitlines():     m=json.loads(inread)     extracted=m['text'].lower()     tokenized= nltk.word_tokenize(extracted)     word in tokenized:         nplist=[]         if my_dict2.get(word) not none:             sequence.append(word)             nplist.append(sequence)             = numpy.array(nplist)             b=counter(a.flat)             z=b.most_common(5)             x.append(z)  sequence[:] nplist[:] b.clear() 

you could explicitly set values in counter 0 following reassingment:

>>> c = counter('xxxy') >>> c = counter({x:0 x in c}) >>> c counter({'y': 0, 'x': 0}) 

... or iterate on keys , set values:

>>> x in c: ...     c[x] = 0 

however, not necessary in cases because __getitem__ method of counter returns 0 default if key cannot found, means c[x] equivalent c.get(x,0) if c counter.

>>> c['z'] 0 

in summary: use clear method:

>>> c.clear() >>> c counter() >>> c['foo'] 0 

Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -