Questions tagged [counter]

A Counter is a container(/bag/multiset/etc.) that keeps track of how many times equivalent values are added.

In Python:

A Counter is a dict subclass for counting hashable objects. It is an unordered collection where elements are stored as dictionary keys and their counts are stored as dictionary values. Counts are allowed to be any integer value including zero or negative counts. The Counter class is similar to bags or multisets in other languages.

http://docs.python.org/library/collections.html#collections.Counter


5262 questions
640
votes
12 answers

Get loop counter/index using for…of syntax in JavaScript

Caution: question still applies to for…of loops.> Don't use for…in to iterate over an Array, use it to iterate over the properties of an object. That said, this I understand that the basic for…in syntax in JavaScript looks like this: for (var…
hobbes3
  • 28,078
  • 24
  • 87
  • 116
220
votes
4 answers

How to sort Counter by value? - python

Other than doing list comprehensions of reversed list comprehension, is there a pythonic way to sort Counter by value? If so, it is faster than this: >>> from collections import Counter >>> x = Counter({'a':5, 'b':3, 'c':7}) >>> sorted(x) ['a', 'b',…
alvas
  • 115,346
  • 109
  • 446
  • 738
209
votes
5 answers

C# Thread safe fast(est) counter

What is the way to obtain a thread safe counter in C# with best possible performance? This is as simple as it gets: public static long GetNextValue() { long result; lock (LOCK) { result = COUNTER++; } return…
JohnDoDo
  • 4,720
  • 8
  • 31
  • 47
163
votes
13 answers

Counter increment in Bash loop not working

I have the following simple script where I am running a loop and want to maintain a COUNTER. I am unable to figure out why the counter is not updating. Is it due to subshell that's getting created? How can I potentially fix…
Sparsh Gupta
  • 2,163
  • 5
  • 19
  • 21
153
votes
13 answers

Code for a simple JavaScript countdown timer?

I want to use a simple countdown timer starting at 30 seconds from when the function is run and ending at 0. No milliseconds. How can it be coded?
Michael Swarts
  • 3,813
  • 8
  • 31
  • 41
135
votes
26 answers

Count characters in textarea

I want to count characters in a textarea, so I just made: function countChar(val){ var len = val.value.length; if (len >= 500) { val.value = val.value.substring(0,…
Kyle
  • 1,385
  • 2
  • 9
  • 8
84
votes
5 answers

Transform a Counter object into a Pandas DataFrame

I used Counter on a list to compute this variable: final = Counter(event_container) print final gives: Counter({'fb_view_listing': 76, 'fb_homescreen': 63, 'rt_view_listing': 50, 'rt_home_start_app': 46, 'fb_view_wishlist': 39, 'fb_view_product':…
woshitom
  • 4,811
  • 8
  • 38
  • 62
79
votes
8 answers

How to count the number of words in a sentence, ignoring numbers, punctuation and whitespace?

How would I go about counting the words in a sentence? I'm using Python. For example, I might have the string: string = "I am having a very nice 23!@$ day. " That would be 7 words. I'm having trouble with the random amount of spaces…
HossBender
  • 1,019
  • 2
  • 10
  • 23
73
votes
1 answer

Summing the contents of two collections.Counter() objects

I am working with collections.Counter() counters. I would like to combine two of them in a meaningful manner. Suppose I have 2 counters, say, Counter({'menu': 20, 'good': 15, 'happy': 10, 'bar': 5}) and Counter({'menu': 1, 'good': 1, 'bar':…
tumultous_rooster
  • 12,150
  • 32
  • 92
  • 149
69
votes
4 answers

Sum of all counts in a collections.Counter

What is the best way of establishing the sum of all counts in a collections.Counter object? I've tried: sum(Counter([1,2,3,4,5,1,2,1,6])) but this gives 21 instead of 9?
Baz
  • 12,713
  • 38
  • 145
  • 268
62
votes
4 answers

How to access CSS generated content with JavaScript

I generate the numbering of my headers and figures with CSS's counter and content properties: img.figure:after { counter-increment: figure; content: "Fig. " counter(section) "." counter(figure); } This (appropriate browser assumed) gives a nice…
Boldewyn
  • 81,211
  • 44
  • 156
  • 212
60
votes
10 answers

Counter in foreach loop in C#

Working of foreach: As I know, foreach is a loop which iterates through a collection or array one by one, starting from 0 index till the last item of the collection. So, if I have n items in an array. foreach (var item in arr) { } then,…
Javed Akram
  • 15,024
  • 26
  • 81
  • 118
58
votes
5 answers

MySQL SELECT increment counter

Here is my MySQL query: SELECT name FROM table; How can I also select an increment counter alongside name? Expected output: Jay 1 roy 2 ravi 3 ram 4
iJade
  • 23,144
  • 56
  • 154
  • 243
57
votes
6 answers

How to get the number of the most frequent value in a column?

I have a data frame and I would like to know how many times a given column has the most frequent value. I try to do it in the following way: items_counts = df['item'].value_counts() max_item = items_counts.max() As a result I get: ValueError:…
Roman
  • 124,451
  • 167
  • 349
  • 456
53
votes
3 answers

How to add or increment single item of the Python Counter class

A set uses .update to add multiple items, and .add to add a single one. Why doesn't collections.Counter work the same way? To increment a single Counter item using Counter.update, it seems like you have to add it to a list: from collections import…
scharfmn
  • 3,561
  • 7
  • 38
  • 53
1
2 3
99 100