Using the Counter function on a list in python -
we have return frequency of length of words in .txt file. e.g "my name emily"
converted list: ["my", "name", "is", "emily"]
, converted list of lengths of each word: [2, 4, 2, 5]
, use function counter outputs dictionary looks like:
counter({2: 2, 4: 1, 5: 1})
but need include count of zero:
counter({1: 0, 2: 2, 3: 0, 4: 1, 5: 1})
any ideas? should rid of counter function together?
counter
counts frequency of items, means keeps count of items present.
but, if item looking not there in counter
object, return 0
default.
for example,
print counter()[1] # 0
if need items 0 count in it, can create normal dictionary out of counter, this
c = counter({2: 2, 4: 1, 5: 1}) print {num:c[num] num in xrange(1, max(c) + 1)} # {1: 0, 2: 2, 3: 0, 4: 1, 5: 1}
Comments
Post a Comment