str_tuple = "abcd",
a = Counter()
a.update(str_tuple)
but a[('abcd',)] == 0
because the Counter
counted the 'abcd'
string, not the tuple. I need to count the tuple.
Counter.update()
takes a sequence of things to count. If you need to count a tuple, put that value into a sequence before passing it to the Counter.update()
method:
a.update([str_tuple])
or use:
a[str_tuple] += 1
to increment the count for that one tuple by one.
Demo:
>>> from collections import Counter
>>> str_tuple = "abcd",
>>> a = Counter()
>>> a.update([str_tuple])
>>> a
Counter({('abcd',): 1})
>>> a = Counter()
>>> a[str_tuple] += 1
>>> a
Counter({('abcd',): 1})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.