counting

How to determine if an array has any elements or not?

谁说胖子不能爱 提交于 2019-11-27 04:35:56
问题 How do I find if an array has one or more elements? I need to execute a block of code where the size of the array is greater than zero. if ($result > 0) { // Here is the code body which I want to execute } else { // Here is some other code } 回答1: You can use the count() or sizeof() PHP functions: if (sizeof($result) > 0) { echo "array size is greater than zero"; } else { echo "array size is zero"; } Or you can use: if (count($result) > 0) { echo "array size is greater than zero"; } else {

Counting the number of True Booleans in a Python List

萝らか妹 提交于 2019-11-27 01:25:55
问题 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 . 回答1: True is equal to 1 . >>> sum([True, True, False, False, False, True]) 3 回答2: list has a

How can we track hashtags with the new facebook hashtag implementation

爷,独闯天下 提交于 2019-11-27 00:50:57
Since Facebook introduced hashtags, I've been interested in finding out more about them. Can someone point me in the direction of possibly tracking the hashtags similar to how twitter allows us access to pull hashtag data via their API. I can count mentions, get usernames, and the tweets. Has Facebook launched anything similar? I can't find any documentation online. Igy There is currently no API for the hashtags feature on Facebook edit : there was however a public posts search function which will return some public posts with a certain hashtag if you use that hashtag as the search string in

Add index to contiguous runs of equal values

岁酱吖の 提交于 2019-11-26 19:05:45
Is there is a faster way to make a counter index than using a loop? Within contiguous runs of equal values, the index should be the same. I find the looping very slow especially when the data is so big. For illustration, here is the input and desired output x <- c(2, 3, 9, 2, 4, 4, 3, 4, 4, 5, 5, 5, 1) Desired resulting counter: c(1, 2, 3, 4, 5, 5, 6, 7, 7, 8, 8, 8, 9) Note that non -contiguous runs have different indexes. E.g. see the desired indexes of the values 2 and 4 My inefficient code is this: group[1]<-1 counter<-1 for (i in 2:n){ if (x[i]==x[i-1]){ group[i]<-counter }else{ counter<

Counting Values in Multidimensional Array

我只是一个虾纸丫 提交于 2019-11-26 17:07:00
问题 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

Fast way of counting non-zero bits in positive integer

半世苍凉 提交于 2019-11-26 12:55:42
I need a fast way to count the number of bits in an integer in python. My current solutions is bin(n).count("1") but I am wondering if there is any faster way of doing this? PS: (i am representing a big 2D binary array as a singe list of numbers and doing bitwise operations, and that brings the time down from hours to minutes. and now i would like to get rid of those extra minutes. Edit: 1. it has to be in python 2.7 or 2.6 and optimizing for small numbers does not matter that much since that would not be a clear bottle neck, but I do have numbers with 10 000 + bits at some places for example

How can we track hashtags with the new facebook hashtag implementation

一世执手 提交于 2019-11-26 09:28:23
问题 Since Facebook introduced hashtags, I\'ve been interested in finding out more about them. Can someone point me in the direction of possibly tracking the hashtags similar to how twitter allows us access to pull hashtag data via their API. I can count mentions, get usernames, and the tweets. Has Facebook launched anything similar? I can\'t find any documentation online. 回答1: There is currently no API for the hashtags feature on Facebook edit : there was however a public posts search function

Add index to contiguous runs of equal values

。_饼干妹妹 提交于 2019-11-26 06:48:07
问题 Is there is a faster way to make a counter index than using a loop? Within contiguous runs of equal values, the index should be the same. I find the looping very slow especially when the data is so big. For illustration, here is the input and desired output x <- c(2, 3, 9, 2, 4, 4, 3, 4, 4, 5, 5, 5, 1) Desired resulting counter: c(1, 2, 3, 4, 5, 5, 6, 7, 7, 8, 8, 8, 9) Note that non -contiguous runs have different indexes. E.g. see the desired indexes of the values 2 and 4 My inefficient code

How to count the frequency of the elements in a list?

烂漫一生 提交于 2019-11-26 03:14:03
问题 I need to find the frequency of elements in a list a = [1,1,1,1,2,2,2,2,3,3,4,5,5] output-> b = [4,4,2,1,2] Also I want to remove the duplicates from a a = [1,2,3,4,5] 回答1: Since the list is ordered you can do this: a = [1,1,1,1,2,2,2,2,3,3,4,5,5] from itertools import groupby [len(list(group)) for key, group in groupby(a)] Output: [4, 4, 2, 1, 2] 回答2: In Python 2.7 (or newer), you can use collections.Counter: import collections a = [1,1,1,1,2,2,2,2,3,3,4,5,5] counter=collections.Counter(a)

Fast way of counting non-zero bits in positive integer

自闭症网瘾萝莉.ら 提交于 2019-11-26 03:09:33
问题 I need a fast way to count the number of bits in an integer in python. My current solution is bin(n).count(\"1\") but I am wondering if there is any faster way of doing this? PS: (i am representing a big 2D binary array as a single list of numbers and doing bitwise operations, and that brings the time down from hours to minutes. and now I would like to get rid of those extra minutes. Edit: 1. it has to be in python 2.7 or 2.6 and optimizing for small numbers does not matter that much since