Counting the number of distinct keys in a dictionary in Python
len(yourdict.keys()) or just len(yourdict) If you like to count unique words in the file, you could just use set and do like len(set(open(yourdictfile).read().split()))
len(yourdict.keys()) or just len(yourdict) If you like to count unique words in the file, you could just use set and do like len(set(open(yourdictfile).read().split()))
You can use an object to hold the results: const arr = [5, 5, 5, 2, 2, 2, 2, 2, 9, 4]; const counts = {}; for (const num of arr) { counts[num] = counts[num] ? counts[num] + 1 : 1; } console.log(counts[5], counts[2], counts[9], counts[4]); So, now your counts object can tell you what … Read more
os.listdir() will be slightly more efficient than using glob.glob. To test if a filename is an ordinary file (and not a directory or other entity), use os.path.isfile(): import os, os.path # simple version for working with CWD print len([name for name in os.listdir(‘.’) if os.path.isfile(name)]) # path joining version for other paths DIR = ‘/tmp’ … Read more
If you want the length of an integer as in the number of digits in the integer, you can always convert it to string like str(133) and find its length like len(str(123)).
I believe this is what you want: table.groupby(‘YEARMONTH’).CLIENTCODE.nunique() Example: In [2]: table Out[2]: CLIENTCODE YEARMONTH 0 1 201301 1 1 201301 2 2 201301 3 1 201302 4 2 201302 5 2 201302 6 3 201302 In [3]: table.groupby(‘YEARMONTH’).CLIENTCODE.nunique() Out[3]: YEARMONTH 201301 2 201302 3
You can just use table(): > a <- table(numbers) > a numbers 4 5 23 34 43 54 56 65 67 324 435 453 456 567 657 2 1 2 2 1 1 2 1 2 1 3 1 1 1 1 Then you can subset it: > a[names(a)==435] 435 3 Or convert it into … Read more
There’s three ways to get this sort of count, each with their own tradeoffs. If you want a true count, you have to execute the SELECT statement like the one you used against each table. This is because PostgreSQL keeps row visibility information in the row itself, not anywhere else, so any accurate count can … Read more
Use a selector that will select all the rows and take the length. var rowCount = $(‘#myTable tr’).length; Note: this approach also counts all trs of every nested table!
Using numpy.unique: import numpy a = numpy.array([0, 3, 0, 1, 0, 1, 2, 1, 0, 0, 0, 0, 1, 3, 4]) unique, counts = numpy.unique(a, return_counts=True) >>> dict(zip(unique, counts)) {0: 7, 1: 4, 2: 1, 3: 2, 4: 1} Non-numpy method using collections.Counter; import collections, numpy a = numpy.array([0, 3, 0, 1, 0, 1, 2, … Read more
Assuming there is one number per line: sort <file> | uniq -c You can use the more verbose –count flag too with the GNU version, e.g., on Linux: sort <file> | uniq –count