Python - Number of occurrence of an event [duplicate] - python

This question already has answers here:
Numpy mask to count number of elements satisfying a condition
(3 answers)
Closed 3 years ago.
I have an array as follow:
a = [1 2 5 3 8 7 2 9 8]
and a constant number b=4
How can I count the occurrence c of a being inferior to b?
So in this example c=4

Using numpy:
np.sum(a < 4)
Or a sum on generator:
sum(num < 4 for num in a)

If you mean "less than" by "inferior", you can use a list comprehension
c = len([x for x in a if x < b])
If you're worried about space constraints, you can use a generator like Alexander's answer.
sum(1 if num < b else 0 for num in a)

Related

Why is 0^1 = 1 in Python? [duplicate]

This question already has answers here:
What does the caret (^) operator do?
(5 answers)
Closed 25 days ago.
I was looking at a problem solution in Python where the only number without a pair in an array is returned. The solution is below :
def solution(A):
n = len(A)
if A is None or n == 0:
return 0
if n == 1:
return A[0]
result = 0
for i in range(0, n):
result ^= A[i]
return result
How is the loop logic returning back the unique number?
The ^ operator in python is the XOR operator
and what it means is that for each a,b:
a^b = a'b + b'a
and in truth table it looks like this:
a
b
a^b
0
0
0
0
1
1
1
0
1
1
1
0

Best way to do range(N) * 2 in python 3 [duplicate]

This question already has answers here:
Is there an elegant way to cycle through a list N times via iteration (like itertools.cycle but limit the cycles)?
(6 answers)
Closed 3 years ago.
I know that python 3 range no longer produces a list, but a range object.
Is there a best way to do the following in python 3?
for i in range(3) * 2:
print(i)
# 0
# 1
# 2
# 0
# 1
# 2
One way to do it without making a list is to use chain.from_iterable and repeat from the itertools module. This uses O(1) extra space.
>>> from itertools import chain, repeat
>>> for i in chain.from_iterable(repeat(range(3), 2)):
... print(i)
...
0
1
2
0
1
2
Just make a list from the range:
for i in list(range(3)) * 2:
print(i)
Result:
0
1
2
0
1
2

How to print i till its less than k while k is decreasing every iteration? [duplicate]

This question already has answers here:
Scope of python variable in for loop
(10 answers)
Closed 3 years ago.
I want to print i and k till i is less than or equal to k.
in C++ the code can be given as:
for(i=0;i<k;i++){
cout<<i<<k;
k--;
}
I am not getting the correct output.
this is my code
k=5
for i in range(k):
print(i,k)
k-=1
the output i get is:
0 5
1 4
2 3
3 2
4 1
but i want to get:
0 5
1 4
2 3
is there someway to use the range() function for this?
For loops in Python are really for-each and suboptimal for your needs. Use while instead:
i = 0; k = 5
while i < k:
print(i,k)
i += 1
k -= 1
k=5
for i in range(k):
print(i,k)
if k<=i:
break
k-=1

I cannot understand the remainder operator for negative values in python [duplicate]

This question already has answers here:
How does the modulo (%) operator work on negative numbers in Python?
(12 answers)
Closed 4 years ago.
for instance,-10 % 3 = 2, this does not make any sense as the definition for % is remainder. Thanks
-10 = 3(-4)+2, hence the remainder is 2 mathematically.
Alternatively notice that that 10 % 3 is equal to 1. Hence the remainder of -10 % 3 should be -1 which is equal to 3-1 = 2 modulo 3.
By the rule that a % b follows the sign of b and |a%b| < b, 2 is returned as the answer.

What exactly does this mean in Python? [duplicate]

This question already has answers here:
What is the result of % in Python?
(20 answers)
Closed 9 years ago.
I was working on a project Euler problem and found this code online:
a = 1
b = 2
s = 0
while b <= 4000000:
if not b % 2:
s += b
a, b = b, a + b
print s
I am not entirely sure what if not b % 2: means and was wondering if someone would mind shedding some light on it for me.
From the docs:
The % (modulo) operator yields the remainder from the division of the first argument by the second.
e.g. 5/2 == 2.5 # or 2 remainder 1, 5%2 == 1
The only time a % operation will come out to 0 (which satisfies not x%y) is if it's evenly divisible. In other words, 4 % 2 == 0 because 2 divides 4 evenly.
In your code, it's saying:
while b is less than 4,000,000:
if b is even:
set s equal to s+b
The modulo % operator returns the remainder of the division of b per 2 (in your case).
So if not b % 2 is the same meaning that if b % 2 == 0: it checks if the number is even.
b % 2 is the remainder after dividing by 2. So it will be 1 if b is odd, or 0 otherwise.
if not b % 2 can thus be translated to "if b is even"
Python interprets the value 1 as True and 0 as False:
>>> 1 == True
True
>>> 0 == True
False
The modulo operation x % y returns the remainder when x is divided by y. Therefore b % 2 will be 0 when b is even, and 1 when b is odd.
Combining all this together, the statement if not b % 2: corresponds to "if b is even".
It's just a cute way of writing if b%2==0. The not operator when passed 0 will return True, and False given anything else.

Categories

Resources