Questions tagged [count]

Count refers to the number of objects in a collection. It's also a commonly-used SQL function that counts the number of rows.

Many programming languages offer some sort of count, length or size fields and/or methods for arrays and collections, e.g. PHP's count($array) or Java's array.length.

COUNT() is also commonly-used SQL function that counts the number of rows in a table. It is an ANSI SQL aggregate function that returns the number of times the argument is encountered per group (or if no non-aggregate columns, per query). Commonly, the argument is specified as * - ie COUNT(*) - simply counting the rows. It can be used to count distinct values of a column like this: COUNT(DISTINCT MY_COLUMN)

Reference

See also:

18879 questions
1757
votes
28 answers

How can I count the occurrences of a list item?

Given an item, how can I count its occurrences in a list in Python?
weakish
  • 23,766
  • 4
  • 44
  • 54
1702
votes
20 answers

How to efficiently count the number of keys/properties of an object in JavaScript?

What's the fastest way to count the number of keys/properties of an object? It it possible to do this without iterating over the object? i.e. without doing var count = 0; for (k in myobj) if (myobj.hasOwnProperty(k)) count++; (Firefox did provide…
mjs
  • 57,072
  • 26
  • 82
  • 114
1052
votes
21 answers

Count the number occurrences of a character in a string

What's the simplest way to count the number of occurrences of a character in a string? e.g. count the number of times 'a' appears in 'Mary had a little lamb'
Mat
  • 67,636
  • 33
  • 83
  • 106
942
votes
8 answers

How to sort a list of objects based on an attribute of the objects?

I've got a list of Python objects that I'd like to sort by an attribute of the objects themselves. The list looks like: >>> ut [, , , , , , ...] Each object has…
Nick Sergeant
  • 29,184
  • 12
  • 34
  • 44
596
votes
7 answers

Find duplicate lines in a file and count how many time each line was duplicated?

Suppose I have a file similar to the following: 123 123 234 234 123 345 I would like to find how many times '123' was duplicated, how many times '234' was duplicated, etc. So ideally, the output would be like: 123 3 234 2 345 1
user839145
  • 6,023
  • 3
  • 13
  • 10
516
votes
11 answers

jQuery: count number of rows in a table

How do I count the number of tr elements within a table using jQuery? I know there is a similar question, but I just want the total rows.
danjan
484
votes
31 answers

How to count the occurrence of certain item in an ndarray?

In Python, I have an ndarray y that is printed as array([0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1]) I'm trying to count how many 0s and how many 1s are there in this array. But when I type y.count(0) or y.count(1), it says numpy.ndarray object has no…
mflowww
  • 5,315
  • 6
  • 15
  • 18
447
votes
17 answers

How do you find the row count for all your tables in Postgres

I'm looking for a way to find the row count for all my tables in Postgres. I know I can do this one table at a time with: SELECT count(*) FROM table_name; but I'd like to see the row count for all the tables and then order by that to get an idea…
mmrobins
  • 10,669
  • 7
  • 37
  • 40
441
votes
19 answers

Counting the number of elements with the values of x in a vector

I have a vector of numbers: numbers <- c(4,23,4,23,5,43,54,56,657,67,67,435, 453,435,324,34,456,56,567,65,34,435) How can I have R count the number of times a value x appears in the vector?
RQuestions
  • 4,413
  • 3
  • 14
  • 4
384
votes
9 answers

How to get multiple counts with one SQL query?

I am wondering how to write this query. I know this actual syntax is bogus, but it will help you understand what I am wanting. I need it in this format, because it is part of a much bigger query. SELECT distributor_id, COUNT(*) AS TOTAL, COUNT(*)…
Crobzilla
  • 3,963
  • 4
  • 14
  • 11
332
votes
8 answers

Pandas count(distinct) equivalent

I am using pandas as a db substitute as I have multiple databases (oracle, mssql, etc) and I am unable to make a sequence of commands to a SQL equivalent. I have a table loaded in a DataFrame with some columns: YEARMONTH, CLIENTCODE, SIZE, .... etc…
Adriano Almeida
  • 4,411
  • 5
  • 18
  • 27
283
votes
26 answers

How to find length of digits in an integer?

In Python, how do you find the number of digits in an integer?
Strigoides
  • 3,439
  • 5
  • 19
  • 25
279
votes
26 answers

How to count the number of files in a directory using Python

I need to count the number of files in a directory using Python. I guess the easiest way is len(glob.glob('*')), but that also counts the directory itself as a file. Is there any way to count only the files in a directory?
prosseek
  • 155,475
  • 189
  • 518
  • 818
270
votes
6 answers

Counting the number of distinct keys in a dictionary in Python

I have a a dictionary mapping keywords to the repetition of the keyword, but I only want a list of distinct words so I wanted to count the number of keywords. Is there a way to count the number of keywords or is there another way I should look for…
Dan
  • 7,343
  • 16
  • 47
  • 51
255
votes
18 answers

Select count(*) from multiple tables

How can I select count(*) from two different tables (call them tab1 and tab2) having as result: Count_1 Count_2 123 456 I've tried this: select count(*) Count_1 from schema.tab1 union all select count(*) Count_2 from schema.tab2 But all I…
user73118
  • 2,561
  • 2
  • 15
  • 4
1
2 3
99 100