counting

Counting occurrences without using collections.Counter

て烟熏妆下的殇ゞ 提交于 2019-11-28 06:40:50
问题 I am trying to retrieve the most frequent and less frequent elements in a list. frequency([13,12,11,13,14,13,7,11,13,14,12,14,14]) My output is: ([7], [13, 14]) I tried it with: import collections s = [13,12,11,13,14,13,7,11,13,14,12,14,14] count = collections.Counter(s) mins = [a for a, b in count.items() if b == min(count.values())] maxes = [a for a, b in count.items() if b == max(count.values())] final_vals = [mins, maxes] But I don't want to use the collections module and try a more logic

Counting the number of True Booleans in a Python List

≯℡__Kan透↙ 提交于 2019-11-28 06:39:34
I have a list of Booleans: [True, True, False, False, False, True] and I am looking for a way to count the number of True in the list (so in the example above, I want the return to be 3 .) I have found examples of looking for the number of occurrences of specific elements, but is there a more efficient way to do it since I'm working with Booleans? I'm thinking of something analogous to all or any . True is equal to 1 . >>> sum([True, True, False, False, False, True]) 3 Mark Tolonen list has a count method: >>> [True,True,False].count(True) 2 This is actually more efficient than sum , as well

Efficiently count the number of occurrences of unique subarrays in NumPy?

早过忘川 提交于 2019-11-28 00:18:53
问题 I have an array of shape (128, 36, 8) and I'd like to find the number of occurrences of the unique subarrays of length 8 in the last dimension. I'm aware of np.unique and np.bincount , but those seem to be for elements rather than subarrays. I've seen this question but it's about finding the first occurrence of a particular subarray, rather than the counts of all unique subarrays. 回答1: The question states that the input array is of shape (128, 36, 8) and we are interested in finding unique

Counting Values in Multidimensional Array

自闭症网瘾萝莉.ら 提交于 2019-11-27 15:11:40
I currently have the following array: Array( [0] => Array ( [user] => Name 1 [group] => 1 ) [1] => Array ( [user] => Name 2 [group] => 1 ) [2] => Array ( [user] => Name 3 [group] => 2 ) [3] => Array ( [user] => Name 4 [group] => 2 ) [4] => Array ( [user] => Name 5 [group] => 3 ) ) I am attempting to create a new array with the various group values as the key, then count how many are in each group to give the following: Array ( [1] => 2 [2] => 2 [3] => 1 ) I have attempted to use the following, however I get undefined index warnings: $newArr = array(); foreach ($details['user_groups'] as $key =

Python- find the item with maximum occurrences in a list

◇◆丶佛笑我妖孽 提交于 2019-11-27 11:18:47
In Python, I have a list: L = [1, 2, 45, 55, 5, 4, 4, 4, 4, 4, 4, 5456, 56, 6, 7, 67] I want to identify the item that occurred the highest number of times. I am able to solve it but I need the fastest way to do so. I know there is a nice Pythonic answer to this. Andrew Clark Here is a defaultdict solution that will work with Python versions 2.5 and above: from collections import defaultdict L = [1,2,45,55,5,4,4,4,4,4,4,5456,56,6,7,67] d = defaultdict(int) for i in L: d[i] += 1 result = max(d.iteritems(), key=lambda x: x[1]) print result # (4, 6) # The number 4 occurs 6 times Note if L = [1, 2

python histogram one-liner

我只是一个虾纸丫 提交于 2019-11-27 10:51:48
There are many ways to write a Python program that computes a histogram. By histogram, I mean a function that counts the occurrence of objects in an iterable and outputs the counts in a dictionary. For example: >>> L = 'abracadabra' >>> histogram(L) {'a': 5, 'b': 2, 'c': 1, 'd': 1, 'r': 2} One way to write this function is: def histogram(L): d = {} for x in L: if x in d: d[x] += 1 else: d[x] = 1 return d Are there more concise ways of writing this function? If we had dictionary comprehensions in Python, we could write: >>> { x: L.count(x) for x in set(L) } but since Python 2.6 doesn't have

Finding the number of digits of an integer

╄→гoц情女王★ 提交于 2019-11-27 09:46:02
问题 This question was migrated from Software Engineering Stack Exchange because it can be answered on Stack Overflow. Migrated 8 years ago . What is the best method to find the number of digits of a positive integer? I have found this 3 basic methods: conversion to string String s = new Integer(t).toString(); int len = s.length(); for loop for(long long int temp = number; temp >= 1;) { temp/=10; decimalPlaces++; } logaritmic calculation digits = floor( log10( number ) ) + 1; where you can

How to count the characters in a string with Batch? [closed]

倖福魔咒の 提交于 2019-11-27 08:48:29
问题 I need to count the characters of an inputed string in Batch. I don't want to use temporary files. Could it be done without them? If yes, explanations of your code would be greatly appreciated. Thanks SO! 回答1: A simple way is to use a function @echo off set "myVar=abcdefg" call :Stringlength result myVar echo %result% exit /b :Stringlength <resultVar> <stringVar> ( setlocal EnableDelayedExpansion set "s=!%~2!#" set "len=0" for %%P in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do ( if "!s:~

Correct output for function that counts occurrences of each digit in a string

与世无争的帅哥 提交于 2019-11-27 08:47:51
问题 I want the output of the code to be something like this if the user enters a string of numbers like let's say... 122033 Enter string of numbers: 122033 0 occurs 1 time 1 occurs 1 time 2 occurs 2 times 3 occurs 2 times def count_digits(s): res = [0]*10 for x in s: res[int(x)] += 1 while 0 in res: res.remove(0) return res def main(): s=input("Enter string of numbers: ") print(count_digits(s)) main() This is the program that I have so far. At it's current state, if a user enters something like

PHP - Count duplicate values within two dimensional array, then display only unique values with the count

倖福魔咒の 提交于 2019-11-27 08:11:53
问题 I've been working on this for a couple days now... and still haven't been able to achieve my desired results. Any help would on this would be greatly appreciated... thank you in advance. I have a multi-array stored in $log, which displays like this when you print_r($log): Array ( [0] => Array ( [0] => Red [1] => Steel ) [1] => Array ( [0] => Red [1] => Wood ) [2] => Array ( [0] => Blue [1] => Wood ) ) Currently I have this: $counts = $log; foreach ($log as $value) { foreach ($value as $k =>